Skip to main content

rustc_mir_transform/
gvn.rs

1//! Global value numbering.
2//!
3//! MIR may contain repeated and/or redundant computations. The objective of this pass is to detect
4//! such redundancies and re-use the already-computed result when possible.
5//!
6//! From those assignments, we construct a mapping `VnIndex -> Vec<(Local, Location)>` of available
7//! values, the locals in which they are stored, and the assignment location.
8//!
9//! We traverse all assignments `x = rvalue` and operands.
10//!
11//! For each SSA one, we compute a symbolic representation of values that are assigned to SSA
12//! locals. This symbolic representation is defined by the `Value` enum. Each produced instance of
13//! `Value` is interned as a `VnIndex`, which allows us to cheaply compute identical values.
14//!
15//! For each non-SSA
16//! one, we compute the `VnIndex` of the rvalue. If this `VnIndex` is associated to a constant, we
17//! replace the rvalue/operand by that constant. Otherwise, if there is an SSA local `y`
18//! associated to this `VnIndex`, and if its definition location strictly dominates the assignment
19//! to `x`, we replace the assignment by `x = y`.
20//!
21//! By opportunity, this pass simplifies some `Rvalue`s based on the accumulated knowledge.
22//!
23//! # Operational semantic
24//!
25//! Operationally, this pass attempts to prove bitwise equality between locals. Given this MIR:
26//! ```ignore (MIR)
27//! _a = some value // has VnIndex i
28//! // some MIR
29//! _b = some other value // also has VnIndex i
30//! ```
31//!
32//! We consider it to be replaceable by:
33//! ```ignore (MIR)
34//! _a = some value // has VnIndex i
35//! // some MIR
36//! _c = some other value // also has VnIndex i
37//! assume(_a bitwise equal to _c) // follows from having the same VnIndex
38//! _b = _a // follows from the `assume`
39//! ```
40//!
41//! Which is simplifiable to:
42//! ```ignore (MIR)
43//! _a = some value // has VnIndex i
44//! // some MIR
45//! _b = _a
46//! ```
47//!
48//! # Handling of references
49//!
50//! We handle references by assigning a different "provenance" index to each Ref/RawPtr rvalue.
51//! This ensure that we do not spuriously merge borrows that should not be merged. For instance:
52//! ```ignore (MIR)
53//! _x = &_a;
54//! _a = 0;
55//! _y = &_a; // cannot be turned into `_y = _x`!
56//! ```
57//!
58//! On top of that, we consider all the derefs of an immutable reference to a freeze type to give
59//! the same value:
60//! ```ignore (MIR)
61//! _a = *_b // _b is &Freeze
62//! _c = *_b // replaced by _c = _a
63//! ```
64//!
65//! # Determinism of constant propagation
66//!
67//! When registering a new `Value`, we attempt to opportunistically evaluate it as a constant.
68//! The evaluated form is inserted in `evaluated` as an `OpTy` or `None` if evaluation failed.
69//!
70//! The difficulty is non-deterministic evaluation of MIR constants. Some `Const` can have
71//! different runtime values each time they are evaluated. This happens with valtrees that
72//! generate a new allocation each time they are used. This is checked by `is_deterministic`.
73//!
74//! Meanwhile, we want to be able to read indirect constants. For instance:
75//! ```
76//! static A: &'static &'static u8 = &&63;
77//! fn foo() -> u8 {
78//!     **A // We want to replace by 63.
79//! }
80//! fn bar() -> u8 {
81//!     b"abc"[1] // We want to replace by 'b'.
82//! }
83//! ```
84//!
85//! The `Value::Constant` variant stores a possibly unevaluated constant. Evaluating that constant
86//! may be non-deterministic. When that happens, we assign a disambiguator to ensure that we do not
87//! merge the constants. See `duplicate_slice` test in `gvn.rs`.
88//!
89//! Conversely, some constants cannot cross function boundaries, which could happen because of
90//! inlining. For instance, constants that contain a fn pointer (`AllocId` pointing to a
91//! `GlobalAlloc::Function`) point to a different symbol in each codegen unit. To avoid this,
92//! when writing constants in MIR, we do not write `Const`s that contain `AllocId`s. This is
93//! checked by `may_have_provenance`. See <https://github.com/rust-lang/rust/issues/128775> for
94//! more information.
95
96use std::borrow::Cow;
97use std::hash::{Hash, Hasher};
98
99use either::Either;
100use itertools::Itertools as _;
101use rustc_abi::{self as abi, BackendRepr, FIRST_VARIANT, FieldIdx, Primitive, Size, VariantIdx};
102use rustc_arena::DroplessArena;
103use rustc_const_eval::const_eval::DummyMachine;
104use rustc_const_eval::interpret::{
105    ImmTy, Immediate, InterpCx, MemPlaceMeta, MemoryKind, OpTy, Projectable, Scalar,
106    intern_const_alloc_for_constprop,
107};
108use rustc_data_structures::fx::FxHasher;
109use rustc_data_structures::graph::dominators::Dominators;
110use rustc_data_structures::hash_table::{Entry, HashTable};
111use rustc_hir::def::DefKind;
112use rustc_index::bit_set::DenseBitSet;
113use rustc_index::{IndexVec, newtype_index};
114use rustc_middle::bug;
115use rustc_middle::mir::interpret::{AllocRange, GlobalAlloc};
116use rustc_middle::mir::visit::*;
117use rustc_middle::mir::*;
118use rustc_middle::ty::layout::HasTypingEnv;
119use rustc_middle::ty::{self, Ty, TyCtxt};
120use rustc_mir_dataflow::{Analysis, ResultsCursor};
121use rustc_span::DUMMY_SP;
122use smallvec::SmallVec;
123use tracing::{debug, instrument, trace};
124
125use crate::ssa::{MaybeUninitializedLocals, SsaLocals};
126
127pub(super) struct GVN;
128
129impl<'tcx> crate::MirPass<'tcx> for GVN {
130    fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
131        sess.mir_opt_level() >= 2
132    }
133
134    #[instrument(level = "trace", skip(self, tcx, body))]
135    fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
136        debug!(def_id = ?body.source.def_id());
137
138        let typing_env = body.typing_env(tcx);
139        let ssa = SsaLocals::new(tcx, body, typing_env);
140        // Clone dominators because we need them while mutating the body.
141        let dominators = body.basic_blocks.dominators().clone();
142
143        let arena = DroplessArena::default();
144        let mut state =
145            VnState::new(tcx, body, typing_env, &ssa, dominators, &body.local_decls, &arena);
146
147        for local in body.args_iter().filter(|&local| ssa.is_ssa(local)) {
148            let opaque = state.new_argument(body.local_decls[local].ty);
149            state.assign(local, opaque);
150        }
151
152        let reverse_postorder = body.basic_blocks.reverse_postorder().to_vec();
153        for bb in reverse_postorder {
154            let data = &mut body.basic_blocks.as_mut_preserves_cfg()[bb];
155            state.visit_basic_block_data(bb, data);
156        }
157
158        // When emitting storage statements, we want to retain the reused locals' storage statements,
159        // as this enables better optimizations. For each local use location, we mark it for storage removal
160        // only if it might be uninitialized at that point.
161        let storage_to_remove = if tcx.sess.emit_lifetime_markers() {
162            let maybe_uninit = MaybeUninitializedLocals
163                .iterate_to_fixpoint(tcx, body, Some("mir_opt::gvn"))
164                .into_results_cursor(body);
165
166            let mut storage_checker = StorageChecker {
167                reused_locals: &state.reused_locals,
168                storage_to_remove: DenseBitSet::new_empty(body.local_decls.len()),
169                maybe_uninit,
170            };
171
172            for (bb, data) in traversal::reachable(body) {
173                storage_checker.visit_basic_block_data(bb, data);
174            }
175
176            storage_checker.storage_to_remove
177        } else {
178            // Remove the storage statements of all the reused locals.
179            state.reused_locals.clone()
180        };
181
182        debug!(?storage_to_remove);
183
184        StorageRemover { tcx, reused_locals: state.reused_locals, storage_to_remove }
185            .visit_body_preserves_cfg(body);
186    }
187
188    fn is_required(&self) -> bool {
189        false
190    }
191}
192
193newtype_index! {
194    /// This represents a `Value` in the symbolic execution.
195    #[debug_format = "_v{}"]
196    struct VnIndex {}
197}
198
199/// Marker type to forbid hashing and comparing opaque values.
200/// This struct should only be constructed by `ValueSet::insert_unique` to ensure we use that
201/// method to create non-unifiable values. It will ICE if used in `ValueSet::insert`.
202#[derive(Copy, Clone, Debug, Eq)]
203struct VnOpaque;
204impl PartialEq for VnOpaque {
205    fn eq(&self, _: &VnOpaque) -> bool {
206        // ICE if we try to compare unique values
207        unreachable!()
208    }
209}
210impl Hash for VnOpaque {
211    fn hash<T: Hasher>(&self, _: &mut T) {
212        // ICE if we try to hash unique values
213        unreachable!()
214    }
215}
216
217#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
218enum AddressKind {
219    Ref(BorrowKind),
220    Address(RawPtrKind),
221}
222
223#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
224enum AddressBase {
225    /// This address is based on this local.
226    Local(Local),
227    /// This address is based on the deref of this pointer.
228    Deref(VnIndex),
229}
230
231#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
232enum Value<'a, 'tcx> {
233    // Root values.
234    /// Used to represent values we know nothing about.
235    Opaque(VnOpaque),
236    /// The value is a argument.
237    Argument(VnOpaque),
238    /// Evaluated or unevaluated constant value.
239    Constant {
240        value: Const<'tcx>,
241        /// Some constants do not have a deterministic value. To avoid merging two instances of the
242        /// same `Const`, we assign them an additional integer index.
243        // `disambiguator` is `None` iff the constant is deterministic.
244        disambiguator: Option<VnOpaque>,
245    },
246
247    // Aggregates.
248    /// An aggregate value, either tuple/closure/struct/enum.
249    /// This does not contain unions, as we cannot reason with the value.
250    Aggregate(VariantIdx, &'a [VnIndex]),
251    /// A union aggregate value.
252    Union(FieldIdx, VnIndex),
253    /// A raw pointer aggregate built from a thin pointer and metadata.
254    RawPtr {
255        /// Thin pointer component. This is field 0 in MIR.
256        pointer: VnIndex,
257        /// Metadata component. This is field 1 in MIR.
258        metadata: VnIndex,
259    },
260    /// This corresponds to a `[value; count]` expression.
261    Repeat(VnIndex, ty::Const<'tcx>),
262    /// The address of a place.
263    Address {
264        base: AddressBase,
265        // We do not use a plain `Place` as we want to be able to reason about indices.
266        // This does not contain any `Deref` projection.
267        projection: &'a [ProjectionElem<VnIndex, Ty<'tcx>>],
268        kind: AddressKind,
269        /// Give each borrow and pointer a different provenance, so we don't merge them.
270        provenance: VnOpaque,
271    },
272
273    // Extractions.
274    /// This is the *value* obtained by projecting another value.
275    Projection(VnIndex, ProjectionElem<VnIndex, ()>),
276    /// Discriminant of the given value.
277    Discriminant(VnIndex),
278
279    // Operations.
280    RuntimeChecks(RuntimeChecks),
281    UnaryOp(UnOp, VnIndex),
282    BinaryOp(BinOp, VnIndex, VnIndex),
283    Cast {
284        kind: CastKind,
285        value: VnIndex,
286    },
287}
288
289/// Stores and deduplicates pairs of `(Value, Ty)` into in `VnIndex` numbered values.
290///
291/// This data structure is mostly a partial reimplementation of `FxIndexMap<VnIndex, (Value, Ty)>`.
292/// We do not use a regular `FxIndexMap` to skip hashing values that are unique by construction,
293/// like opaque values, address with provenance and non-deterministic constants.
294struct ValueSet<'a, 'tcx> {
295    indices: HashTable<VnIndex>,
296    hashes: IndexVec<VnIndex, u64>,
297    values: IndexVec<VnIndex, Value<'a, 'tcx>>,
298    types: IndexVec<VnIndex, Ty<'tcx>>,
299}
300
301impl<'a, 'tcx> ValueSet<'a, 'tcx> {
302    fn new(num_values: usize) -> ValueSet<'a, 'tcx> {
303        ValueSet {
304            indices: HashTable::with_capacity(num_values),
305            hashes: IndexVec::with_capacity(num_values),
306            values: IndexVec::with_capacity(num_values),
307            types: IndexVec::with_capacity(num_values),
308        }
309    }
310
311    /// Insert a `(Value, Ty)` pair without hashing or deduplication.
312    /// This always creates a new `VnIndex`.
313    #[inline]
314    fn insert_unique(
315        &mut self,
316        ty: Ty<'tcx>,
317        value: impl FnOnce(VnOpaque) -> Value<'a, 'tcx>,
318    ) -> VnIndex {
319        let value = value(VnOpaque);
320
321        debug_assert!(match value {
322            Value::Opaque(_) | Value::Argument(_) | Value::Address { .. } => true,
323            Value::Constant { disambiguator, .. } => disambiguator.is_some(),
324            _ => false,
325        });
326
327        let index = self.hashes.push(0);
328        let _index = self.types.push(ty);
329        debug_assert_eq!(index, _index);
330        let _index = self.values.push(value);
331        debug_assert_eq!(index, _index);
332        index
333    }
334
335    /// Insert a `(Value, Ty)` pair to be deduplicated.
336    /// Returns `true` as second tuple field if this value did not exist previously.
337    #[allow(rustc::disallowed_pass_by_ref)] // closures take `&VnIndex`
338    fn insert(&mut self, ty: Ty<'tcx>, value: Value<'a, 'tcx>) -> (VnIndex, bool) {
339        debug_assert!(match value {
340            Value::Opaque(_) | Value::Address { .. } => false,
341            Value::Constant { disambiguator, .. } => disambiguator.is_none(),
342            _ => true,
343        });
344
345        let hash: u64 = {
346            let mut h = FxHasher::default();
347            value.hash(&mut h);
348            ty.hash(&mut h);
349            h.finish()
350        };
351
352        let eq = |index: &VnIndex| self.values[*index] == value && self.types[*index] == ty;
353        let hasher = |index: &VnIndex| self.hashes[*index];
354        match self.indices.entry(hash, eq, hasher) {
355            Entry::Occupied(entry) => {
356                let index = *entry.get();
357                (index, false)
358            }
359            Entry::Vacant(entry) => {
360                let index = self.hashes.push(hash);
361                entry.insert(index);
362                let _index = self.values.push(value);
363                debug_assert_eq!(index, _index);
364                let _index = self.types.push(ty);
365                debug_assert_eq!(index, _index);
366                (index, true)
367            }
368        }
369    }
370
371    /// Return the `Value` associated with the given `VnIndex`.
372    #[inline]
373    fn value(&self, index: VnIndex) -> Value<'a, 'tcx> {
374        self.values[index]
375    }
376
377    /// Return the type associated with the given `VnIndex`.
378    #[inline]
379    fn ty(&self, index: VnIndex) -> Ty<'tcx> {
380        self.types[index]
381    }
382}
383
384struct VnState<'body, 'a, 'tcx> {
385    tcx: TyCtxt<'tcx>,
386    ecx: InterpCx<'tcx, DummyMachine>,
387    local_decls: &'body LocalDecls<'tcx>,
388    is_coroutine: bool,
389    /// Value stored in each local.
390    locals: IndexVec<Local, Option<VnIndex>>,
391    /// Locals that are assigned that value.
392    // This vector does not hold all the values of `VnIndex` that we create.
393    rev_locals: IndexVec<VnIndex, SmallVec<[Local; 1]>>,
394    values: ValueSet<'a, 'tcx>,
395    /// Values evaluated as constants if possible.
396    /// - `None` are values not computed yet;
397    /// - `Some(None)` are values for which computation has failed;
398    /// - `Some(Some(op))` are successful computations.
399    evaluated: IndexVec<VnIndex, Option<Option<&'a OpTy<'tcx>>>>,
400    ssa: &'body SsaLocals,
401    dominators: Dominators<BasicBlock>,
402    reused_locals: DenseBitSet<Local>,
403    arena: &'a DroplessArena,
404}
405
406impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
407    fn new(
408        tcx: TyCtxt<'tcx>,
409        body: &Body<'tcx>,
410        typing_env: ty::TypingEnv<'tcx>,
411        ssa: &'body SsaLocals,
412        dominators: Dominators<BasicBlock>,
413        local_decls: &'body LocalDecls<'tcx>,
414        arena: &'a DroplessArena,
415    ) -> Self {
416        // Compute a rough estimate of the number of values in the body from the number of
417        // statements. This is meant to reduce the number of allocations, but it's all right if
418        // we miss the exact amount. We estimate based on 2 values per statement (one in LHS and
419        // one in RHS) and 4 values per terminator (for call operands).
420        let num_values =
421            2 * body.basic_blocks.iter().map(|bbdata| bbdata.statements.len()).sum::<usize>()
422                + 4 * body.basic_blocks.len();
423        VnState {
424            tcx,
425            ecx: InterpCx::new(tcx, DUMMY_SP, typing_env, DummyMachine),
426            local_decls,
427            is_coroutine: body.coroutine.is_some(),
428            locals: IndexVec::from_elem(None, local_decls),
429            rev_locals: IndexVec::with_capacity(num_values),
430            values: ValueSet::new(num_values),
431            evaluated: IndexVec::with_capacity(num_values),
432            ssa,
433            dominators,
434            reused_locals: DenseBitSet::new_empty(local_decls.len()),
435            arena,
436        }
437    }
438
439    fn typing_env(&self) -> ty::TypingEnv<'tcx> {
440        self.ecx.typing_env()
441    }
442
443    fn insert_unique(
444        &mut self,
445        ty: Ty<'tcx>,
446        value: impl FnOnce(VnOpaque) -> Value<'a, 'tcx>,
447    ) -> VnIndex {
448        let index = self.values.insert_unique(ty, value);
449        let _index = self.evaluated.push(None);
450        debug_assert_eq!(index, _index);
451        let _index = self.rev_locals.push(SmallVec::new());
452        debug_assert_eq!(index, _index);
453        index
454    }
455
456    #[instrument(level = "trace", skip(self), ret)]
457    fn insert(&mut self, ty: Ty<'tcx>, value: Value<'a, 'tcx>) -> VnIndex {
458        let (index, new) = self.values.insert(ty, value);
459        if new {
460            // Grow `evaluated` and `rev_locals` here to amortize the allocations.
461            let _index = self.evaluated.push(None);
462            debug_assert_eq!(index, _index);
463            let _index = self.rev_locals.push(SmallVec::new());
464            debug_assert_eq!(index, _index);
465        }
466        index
467    }
468
469    /// Create a new `Value` for which we have no information at all, except that it is distinct
470    /// from all the others.
471    #[instrument(level = "trace", skip(self), ret)]
472    fn new_opaque(&mut self, ty: Ty<'tcx>) -> VnIndex {
473        let index = self.insert_unique(ty, Value::Opaque);
474        self.evaluated[index] = Some(None);
475        index
476    }
477
478    #[instrument(level = "trace", skip(self), ret)]
479    fn new_argument(&mut self, ty: Ty<'tcx>) -> VnIndex {
480        let index = self.insert_unique(ty, Value::Argument);
481        self.evaluated[index] = Some(None);
482        index
483    }
484
485    /// Create a new `Value::Address` distinct from all the others.
486    #[instrument(level = "trace", skip(self), ret)]
487    fn new_pointer(&mut self, place: Place<'tcx>, kind: AddressKind) -> Option<VnIndex> {
488        let pty = place.ty(self.local_decls, self.tcx).ty;
489        let ty = match kind {
490            AddressKind::Ref(bk) => {
491                Ty::new_ref(self.tcx, self.tcx.lifetimes.re_erased, pty, bk.to_mutbl_lossy())
492            }
493            AddressKind::Address(mutbl) => Ty::new_ptr(self.tcx, pty, mutbl.to_mutbl_lossy()),
494        };
495
496        let mut projection = place.projection.iter();
497        let base = if place.is_indirect_first_projection() {
498            let base = self.locals[place.local]?;
499            // Skip the initial `Deref`.
500            projection.next();
501            AddressBase::Deref(base)
502        } else if self.ssa.is_ssa(place.local) {
503            // Only propagate the pointer of the SSA local.
504            AddressBase::Local(place.local)
505        } else {
506            return None;
507        };
508        // Do not try evaluating inside `Index`, this has been done by `simplify_place_projection`.
509        let projection =
510            projection.map(|proj| proj.try_map(|index| self.locals[index], |ty| ty).ok_or(()));
511        let projection = self.arena.try_alloc_from_iter(projection).ok()?;
512
513        let index = self.insert_unique(ty, |provenance| Value::Address {
514            base,
515            projection,
516            kind,
517            provenance,
518        });
519        Some(index)
520    }
521
522    #[instrument(level = "trace", skip(self), ret)]
523    fn insert_constant(&mut self, value: Const<'tcx>) -> VnIndex {
524        if is_deterministic(value) {
525            // The constant is deterministic, no need to disambiguate.
526            let constant = Value::Constant { value, disambiguator: None };
527            self.insert(value.ty(), constant)
528        } else {
529            // Multiple mentions of this constant will yield different values,
530            // so assign a different `disambiguator` to ensure they do not get the same `VnIndex`.
531            self.insert_unique(value.ty(), |disambiguator| Value::Constant {
532                value,
533                disambiguator: Some(disambiguator),
534            })
535        }
536    }
537
538    #[inline]
539    fn get(&self, index: VnIndex) -> Value<'a, 'tcx> {
540        self.values.value(index)
541    }
542
543    #[inline]
544    fn ty(&self, index: VnIndex) -> Ty<'tcx> {
545        self.values.ty(index)
546    }
547
548    /// Record that `local` is assigned `value`. `local` must be SSA.
549    #[instrument(level = "trace", skip(self))]
550    fn assign(&mut self, local: Local, value: VnIndex) {
551        debug_assert!(self.ssa.is_ssa(local));
552        self.locals[local] = Some(value);
553        self.rev_locals[value].push(local);
554    }
555
556    fn insert_bool(&mut self, flag: bool) -> VnIndex {
557        // Booleans are deterministic.
558        let value = Const::from_bool(self.tcx, flag);
559        debug_assert!(is_deterministic(value));
560        self.insert(self.tcx.types.bool, Value::Constant { value, disambiguator: None })
561    }
562
563    fn insert_scalar(&mut self, ty: Ty<'tcx>, scalar: Scalar) -> VnIndex {
564        // Scalars are deterministic.
565        let value = Const::from_scalar(self.tcx, scalar, ty);
566        debug_assert!(is_deterministic(value));
567        self.insert(ty, Value::Constant { value, disambiguator: None })
568    }
569
570    fn insert_tuple(&mut self, ty: Ty<'tcx>, values: &[VnIndex]) -> VnIndex {
571        self.insert(ty, Value::Aggregate(VariantIdx::ZERO, self.arena.alloc_slice(values)))
572    }
573
574    #[instrument(level = "trace", skip(self), ret)]
575    fn eval_to_const_inner(&mut self, value: VnIndex) -> Option<OpTy<'tcx>> {
576        use Value::*;
577        let ty = self.ty(value);
578        // Avoid computing layouts inside a coroutine, as that can cause cycles.
579        let ty = if !self.is_coroutine || ty.is_scalar() {
580            self.ecx.layout_of(ty).ok()?
581        } else {
582            return None;
583        };
584        let op = match self.get(value) {
585            _ if ty.is_zst() => ImmTy::uninit(ty).into(),
586
587            Opaque(_) | Argument(_) => return None,
588            // Keep runtime check constants as symbolic.
589            RuntimeChecks(..) => return None,
590
591            // In general, evaluating repeat expressions just consumes a lot of memory.
592            // But in the special case that the element is just Immediate::Uninit, we can evaluate
593            // it without extra memory! If we don't propagate uninit values like this, LLVM can get
594            // very confused: https://github.com/rust-lang/rust/issues/139355
595            Repeat(value, _count) => {
596                let value = self.eval_to_const(value)?;
597                if value.is_immediate_uninit() {
598                    ImmTy::uninit(ty).into()
599                } else {
600                    return None;
601                }
602            }
603            Constant { ref value, disambiguator: _ } => {
604                self.ecx.eval_mir_constant(value, DUMMY_SP, None).discard_err()?
605            }
606            Aggregate(variant, ref fields) => {
607                let fields =
608                    fields.iter().map(|&f| self.eval_to_const(f)).collect::<Option<Vec<_>>>()?;
609                let variant = if ty.ty.is_enum() { Some(variant) } else { None };
610                let (BackendRepr::Scalar(..) | BackendRepr::ScalarPair(..)) = ty.backend_repr
611                else {
612                    return None;
613                };
614                let dest = self.ecx.allocate(ty, MemoryKind::Stack).discard_err()?;
615                let variant_dest = if let Some(variant) = variant {
616                    self.ecx.project_downcast(&dest, variant).discard_err()?
617                } else {
618                    dest.clone()
619                };
620                for (field_index, op) in fields.into_iter().enumerate() {
621                    let field_dest = self
622                        .ecx
623                        .project_field(&variant_dest, FieldIdx::from_usize(field_index))
624                        .discard_err()?;
625                    self.ecx.copy_op(op, &field_dest).discard_err()?;
626                }
627                self.ecx
628                    .write_discriminant(variant.unwrap_or(FIRST_VARIANT), &dest)
629                    .discard_err()?;
630                self.ecx
631                    .alloc_mark_immutable(dest.ptr().provenance.unwrap().alloc_id())
632                    .discard_err()?;
633                dest.into()
634            }
635            Union(active_field, field) => {
636                let field = self.eval_to_const(field)?;
637                if field.layout.layout.is_zst() {
638                    ImmTy::from_immediate(Immediate::Uninit, ty).into()
639                } else if matches!(
640                    ty.backend_repr,
641                    BackendRepr::Scalar(..) | BackendRepr::ScalarPair(..)
642                ) {
643                    let dest = self.ecx.allocate(ty, MemoryKind::Stack).discard_err()?;
644                    let field_dest = self.ecx.project_field(&dest, active_field).discard_err()?;
645                    self.ecx.copy_op(field, &field_dest).discard_err()?;
646                    self.ecx
647                        .alloc_mark_immutable(dest.ptr().provenance.unwrap().alloc_id())
648                        .discard_err()?;
649                    dest.into()
650                } else {
651                    return None;
652                }
653            }
654            RawPtr { pointer, metadata } => {
655                let pointer = self.eval_to_const(pointer)?;
656                let metadata = self.eval_to_const(metadata)?;
657
658                // Pointers don't have fields, so don't `project_field` them.
659                let data = self.ecx.read_pointer(pointer).discard_err()?;
660                let meta = if metadata.layout.is_zst() {
661                    MemPlaceMeta::None
662                } else {
663                    MemPlaceMeta::Meta(self.ecx.read_scalar(metadata).discard_err()?)
664                };
665                let ptr_imm = Immediate::new_pointer_with_meta(data, meta, &self.ecx);
666                ImmTy::from_immediate(ptr_imm, ty).into()
667            }
668
669            Projection(base, elem) => {
670                let base = self.eval_to_const(base)?;
671                // `Index` by constants should have been replaced by `ConstantIndex` by
672                // `simplify_place_projection`.
673                let elem = elem.try_map(|_| None, |()| ty.ty)?;
674                self.ecx.project(base, elem).discard_err()?
675            }
676            Address { base, projection, .. } => {
677                debug_assert!(!projection.contains(&ProjectionElem::Deref));
678                let pointer = match base {
679                    AddressBase::Deref(pointer) => self.eval_to_const(pointer)?,
680                    // We have no stack to point to.
681                    AddressBase::Local(_) => return None,
682                };
683                let mut mplace = self.ecx.deref_pointer(pointer).discard_err()?;
684                for elem in projection {
685                    // `Index` by constants should have been replaced by `ConstantIndex` by
686                    // `simplify_place_projection`.
687                    let elem = elem.try_map(|_| None, |ty| ty)?;
688                    mplace = self.ecx.project(&mplace, elem).discard_err()?;
689                }
690                let pointer = mplace.to_ref(&self.ecx);
691                ImmTy::from_immediate(pointer, ty).into()
692            }
693
694            Discriminant(base) => {
695                let base = self.eval_to_const(base)?;
696                let variant = self.ecx.read_discriminant(base).discard_err()?;
697                let discr_value =
698                    self.ecx.discriminant_for_variant(base.layout.ty, variant).discard_err()?;
699                discr_value.into()
700            }
701            UnaryOp(un_op, operand) => {
702                let operand = self.eval_to_const(operand)?;
703                let operand = self.ecx.read_immediate(operand).discard_err()?;
704                let val = self.ecx.unary_op(un_op, &operand).discard_err()?;
705                val.into()
706            }
707            BinaryOp(bin_op, lhs, rhs) => {
708                let lhs = self.eval_to_const(lhs)?;
709                let rhs = self.eval_to_const(rhs)?;
710                let lhs = self.ecx.read_immediate(lhs).discard_err()?;
711                let rhs = self.ecx.read_immediate(rhs).discard_err()?;
712                let val = self.ecx.binary_op(bin_op, &lhs, &rhs).discard_err()?;
713                val.into()
714            }
715            Cast { kind, value } => match kind {
716                CastKind::IntToInt | CastKind::IntToFloat => {
717                    let value = self.eval_to_const(value)?;
718                    let value = self.ecx.read_immediate(value).discard_err()?;
719                    let res = self.ecx.int_to_int_or_float(&value, ty).discard_err()?;
720                    res.into()
721                }
722                CastKind::FloatToFloat | CastKind::FloatToInt => {
723                    let value = self.eval_to_const(value)?;
724                    let value = self.ecx.read_immediate(value).discard_err()?;
725                    let res = self.ecx.float_to_float_or_int(&value, ty).discard_err()?;
726                    res.into()
727                }
728                CastKind::Transmute | CastKind::Subtype => {
729                    let value = self.eval_to_const(value)?;
730                    // `offset` for immediates generally only supports projections that match the
731                    // type of the immediate. However, as a HACK, we exploit that it can also do
732                    // limited transmutes: it only works between types with the same layout, and
733                    // cannot transmute pointers to integers.
734                    if value.as_mplace_or_imm().is_right() {
735                        let can_transmute = match (value.layout.backend_repr, ty.backend_repr) {
736                            (BackendRepr::Scalar(s1), BackendRepr::Scalar(s2)) => {
737                                s1.size(&self.ecx) == s2.size(&self.ecx)
738                                    && !matches!(s1.primitive(), Primitive::Pointer(..))
739                            }
740                            (BackendRepr::ScalarPair(a1, b1), BackendRepr::ScalarPair(a2, b2)) => {
741                                a1.size(&self.ecx) == a2.size(&self.ecx)
742                                    && b1.size(&self.ecx) == b2.size(&self.ecx)
743                                    // The alignment of the second component determines its offset, so that also needs to match.
744                                    && b1.align(&self.ecx) == b2.align(&self.ecx)
745                                    // None of the inputs may be a pointer.
746                                    && !matches!(a1.primitive(), Primitive::Pointer(..))
747                                    && !matches!(b1.primitive(), Primitive::Pointer(..))
748                            }
749                            _ => false,
750                        };
751                        if !can_transmute {
752                            return None;
753                        }
754                    }
755                    value.offset(Size::ZERO, ty, &self.ecx).discard_err()?
756                }
757                CastKind::PointerCoercion(ty::adjustment::PointerCoercion::Unsize, _) => {
758                    let src = self.eval_to_const(value)?;
759                    let dest = self.ecx.allocate(ty, MemoryKind::Stack).discard_err()?;
760                    self.ecx.unsize_into(src, ty, &dest).discard_err()?;
761                    self.ecx
762                        .alloc_mark_immutable(dest.ptr().provenance.unwrap().alloc_id())
763                        .discard_err()?;
764                    dest.into()
765                }
766                CastKind::FnPtrToPtr | CastKind::PtrToPtr => {
767                    let src = self.eval_to_const(value)?;
768                    let src = self.ecx.read_immediate(src).discard_err()?;
769                    let ret = self.ecx.ptr_to_ptr(&src, ty).discard_err()?;
770                    ret.into()
771                }
772                CastKind::PointerCoercion(ty::adjustment::PointerCoercion::UnsafeFnPointer, _) => {
773                    let src = self.eval_to_const(value)?;
774                    let src = self.ecx.read_immediate(src).discard_err()?;
775                    ImmTy::from_immediate(*src, ty).into()
776                }
777                _ => return None,
778            },
779        };
780        Some(op)
781    }
782
783    fn eval_to_const(&mut self, index: VnIndex) -> Option<&'a OpTy<'tcx>> {
784        if let Some(op) = self.evaluated[index] {
785            return op;
786        }
787        let op = self.eval_to_const_inner(index);
788        self.evaluated[index] = Some(self.arena.alloc(op).as_ref());
789        self.evaluated[index].unwrap()
790    }
791
792    /// Represent the *value* we obtain by dereferencing an `Address` value.
793    #[instrument(level = "trace", skip(self), ret)]
794    fn dereference_address(
795        &mut self,
796        base: AddressBase,
797        projection: &[ProjectionElem<VnIndex, Ty<'tcx>>],
798    ) -> Option<VnIndex> {
799        let (mut place_ty, mut value) = match base {
800            // The base is a local, so we take the local's value and project from it.
801            AddressBase::Local(local) => {
802                let local = self.locals[local]?;
803                let place_ty = PlaceTy::from_ty(self.ty(local));
804                (place_ty, local)
805            }
806            // The base is a pointer's deref, so we introduce the implicit deref.
807            AddressBase::Deref(reborrow) => {
808                let place_ty = PlaceTy::from_ty(self.ty(reborrow));
809                self.project(place_ty, reborrow, ProjectionElem::Deref)?
810            }
811        };
812        for &proj in projection {
813            (place_ty, value) = self.project(place_ty, value, proj)?;
814        }
815        Some(value)
816    }
817
818    #[instrument(level = "trace", skip(self), ret)]
819    fn project(
820        &mut self,
821        place_ty: PlaceTy<'tcx>,
822        value: VnIndex,
823        proj: ProjectionElem<VnIndex, Ty<'tcx>>,
824    ) -> Option<(PlaceTy<'tcx>, VnIndex)> {
825        let projection_ty = place_ty.projection_ty(self.tcx, proj);
826        let proj = match proj {
827            ProjectionElem::Deref => {
828                if let Some(Mutability::Not) = place_ty.ty.ref_mutability()
829                    && projection_ty.ty.is_freeze(self.tcx, self.typing_env())
830                {
831                    if let Value::Address { base, projection, .. } = self.get(value)
832                        && let Some(value) = self.dereference_address(base, projection)
833                    {
834                        return Some((projection_ty, value));
835                    }
836                    // DO NOT reason the pointer value.
837                    // We cannot unify two pointers that dereference same local, because they may
838                    // have different lifetimes.
839                    // ```
840                    // let b: &T = *a;
841                    // ... `a` is allowed to be modified. `c` and `b` have different borrowing lifetime.
842                    // Unifying them will extend the lifetime of `b`.
843                    // let c: &T = *a;
844                    // ```
845                    if projection_ty.ty.is_ref() {
846                        return None;
847                    }
848
849                    // An immutable borrow `_x` always points to the same value for the
850                    // lifetime of the borrow, so we can merge all instances of `*_x`.
851                    let deref = self
852                        .insert(projection_ty.ty, Value::Projection(value, ProjectionElem::Deref));
853                    return Some((projection_ty, deref));
854                } else {
855                    return None;
856                }
857            }
858            ProjectionElem::Downcast(name, index) => ProjectionElem::Downcast(name, index),
859            ProjectionElem::Field(f, _) => match self.get(value) {
860                Value::Aggregate(_, fields) => return Some((projection_ty, fields[f.as_usize()])),
861                Value::Union(active, field) if active == f => return Some((projection_ty, field)),
862                Value::Projection(outer_value, ProjectionElem::Downcast(_, read_variant))
863                    if let Value::Aggregate(written_variant, fields) = self.get(outer_value)
864                    // This pass is not aware of control-flow, so we do not know whether the
865                    // replacement we are doing is actually reachable. We could be in any arm of
866                    // ```
867                    // match Some(x) {
868                    //     Some(y) => /* stuff */,
869                    //     None => /* other */,
870                    // }
871                    // ```
872                    //
873                    // In surface rust, the current statement would be unreachable.
874                    //
875                    // However, from the reference chapter on enums and RFC 2195,
876                    // accessing the wrong variant is not UB if the enum has repr.
877                    // So it's not impossible for a series of MIR opts to generate
878                    // a downcast to an inactive variant.
879                    && written_variant == read_variant =>
880                {
881                    return Some((projection_ty, fields[f.as_usize()]));
882                }
883                _ => ProjectionElem::Field(f, ()),
884            },
885            ProjectionElem::Index(idx) => {
886                if let Value::Repeat(inner, _) = self.get(value) {
887                    return Some((projection_ty, inner));
888                }
889                ProjectionElem::Index(idx)
890            }
891            ProjectionElem::ConstantIndex { offset, min_length, from_end } => {
892                match self.get(value) {
893                    Value::Repeat(inner, _) => {
894                        return Some((projection_ty, inner));
895                    }
896                    Value::Aggregate(_, operands) => {
897                        let offset = if from_end {
898                            operands.len() - offset as usize
899                        } else {
900                            offset as usize
901                        };
902                        let value = operands.get(offset).copied()?;
903                        return Some((projection_ty, value));
904                    }
905                    _ => {}
906                };
907                ProjectionElem::ConstantIndex { offset, min_length, from_end }
908            }
909            ProjectionElem::Subslice { from, to, from_end } => {
910                ProjectionElem::Subslice { from, to, from_end }
911            }
912            ProjectionElem::OpaqueCast(_) => ProjectionElem::OpaqueCast(()),
913            ProjectionElem::UnwrapUnsafeBinder(_) => ProjectionElem::UnwrapUnsafeBinder(()),
914        };
915
916        let value = self.insert(projection_ty.ty, Value::Projection(value, proj));
917        Some((projection_ty, value))
918    }
919
920    /// Simplify the projection chain if we know better.
921    #[instrument(level = "trace", skip(self))]
922    fn simplify_place_projection(&mut self, place: &mut Place<'tcx>, location: Location) {
923        // If the projection is indirect, we treat the local as a value, so can replace it with
924        // another local.
925        if place.is_indirect_first_projection()
926            && let Some(base) = self.locals[place.local]
927            && let Some(new_local) = self.try_as_local(base, location)
928            && place.local != new_local
929        {
930            place.local = new_local;
931            self.reused_locals.insert(new_local);
932        }
933
934        let mut projection = Cow::Borrowed(&place.projection[..]);
935
936        for i in 0..projection.len() {
937            let elem = projection[i];
938            if let ProjectionElem::Index(idx_local) = elem
939                && let Some(idx) = self.locals[idx_local]
940            {
941                if let Some(offset) = self.eval_to_const(idx)
942                    && let Some(offset) = self.ecx.read_target_usize(offset).discard_err()
943                    && let Some(min_length) = offset.checked_add(1)
944                {
945                    projection.to_mut()[i] =
946                        ProjectionElem::ConstantIndex { offset, min_length, from_end: false };
947                } else if let Some(new_idx_local) = self.try_as_local(idx, location)
948                    && idx_local != new_idx_local
949                {
950                    projection.to_mut()[i] = ProjectionElem::Index(new_idx_local);
951                    self.reused_locals.insert(new_idx_local);
952                }
953            }
954        }
955
956        if Cow::is_owned(&projection) {
957            place.projection = self.tcx.mk_place_elems(&projection);
958        }
959
960        trace!(?place);
961    }
962
963    /// Represent the *value* which would be read from `place`. If we succeed, return it.
964    /// If we fail, return a `PlaceRef` that contains the same value.
965    #[instrument(level = "trace", skip(self), ret)]
966    fn compute_place_value(
967        &mut self,
968        place: Place<'tcx>,
969        location: Location,
970    ) -> Result<VnIndex, PlaceRef<'tcx>> {
971        // Invariant: `place` and `place_ref` point to the same value, even if they point to
972        // different memory locations.
973        let mut place_ref = place.as_ref();
974
975        // Invariant: `value` holds the value up-to the `index`th projection excluded.
976        let Some(mut value) = self.locals[place.local] else { return Err(place_ref) };
977        // Invariant: `value` has type `place_ty`, with optional downcast variant if needed.
978        let mut place_ty = PlaceTy::from_ty(self.local_decls[place.local].ty);
979        for (index, proj) in place.projection.iter().enumerate() {
980            if let Some(local) = self.try_as_local(value, location) {
981                // Both `local` and `Place { local: place.local, projection: projection[..index] }`
982                // hold the same value. Therefore, following place holds the value in the original
983                // `place`.
984                place_ref = PlaceRef { local, projection: &place.projection[index..] };
985            }
986
987            let Some(proj) = proj.try_map(|value| self.locals[value], |ty| ty) else {
988                return Err(place_ref);
989            };
990            let Some(ty_and_value) = self.project(place_ty, value, proj) else {
991                return Err(place_ref);
992            };
993            (place_ty, value) = ty_and_value;
994        }
995
996        Ok(value)
997    }
998
999    /// Represent the *value* which would be read from `place`, and point `place` to a preexisting
1000    /// place with the same value (if that already exists).
1001    #[instrument(level = "trace", skip(self), ret)]
1002    fn simplify_place_value(
1003        &mut self,
1004        place: &mut Place<'tcx>,
1005        location: Location,
1006    ) -> Option<VnIndex> {
1007        self.simplify_place_projection(place, location);
1008
1009        match self.compute_place_value(*place, location) {
1010            Ok(value) => {
1011                if let Some(new_place) = self.try_as_place(value, location, true)
1012                    && (new_place.local != place.local
1013                        || new_place.projection.len() < place.projection.len())
1014                {
1015                    *place = new_place;
1016                    self.reused_locals.insert(new_place.local);
1017                }
1018                Some(value)
1019            }
1020            Err(place_ref) => {
1021                if place_ref.local != place.local
1022                    || place_ref.projection.len() < place.projection.len()
1023                {
1024                    // By the invariant on `place_ref`.
1025                    *place = place_ref.project_deeper(&[], self.tcx);
1026                    self.reused_locals.insert(place_ref.local);
1027                }
1028                None
1029            }
1030        }
1031    }
1032
1033    #[instrument(level = "trace", skip(self), ret)]
1034    fn simplify_operand(
1035        &mut self,
1036        operand: &mut Operand<'tcx>,
1037        location: Location,
1038    ) -> Option<VnIndex> {
1039        let value = match *operand {
1040            Operand::RuntimeChecks(c) => self.insert(self.tcx.types.bool, Value::RuntimeChecks(c)),
1041            Operand::Constant(ref constant) => self.insert_constant(constant.const_),
1042            Operand::Copy(ref mut place) | Operand::Move(ref mut place) => {
1043                self.simplify_place_value(place, location)?
1044            }
1045        };
1046        if let Some(const_) = self.try_as_constant(value) {
1047            *operand = Operand::Constant(Box::new(const_));
1048        } else if let Value::RuntimeChecks(c) = self.get(value) {
1049            *operand = Operand::RuntimeChecks(c);
1050        }
1051        Some(value)
1052    }
1053
1054    #[instrument(level = "trace", skip(self), ret)]
1055    fn simplify_rvalue(
1056        &mut self,
1057        lhs: &Place<'tcx>,
1058        rvalue: &mut Rvalue<'tcx>,
1059        location: Location,
1060    ) -> Option<VnIndex> {
1061        let value = match *rvalue {
1062            // Forward values.
1063            Rvalue::Use(ref mut operand) => return self.simplify_operand(operand, location),
1064
1065            // Roots.
1066            Rvalue::Repeat(ref mut op, amount) => {
1067                let op = self.simplify_operand(op, location)?;
1068                Value::Repeat(op, amount)
1069            }
1070            Rvalue::Aggregate(..) => return self.simplify_aggregate(rvalue, location),
1071            Rvalue::Ref(_, borrow_kind, ref mut place) => {
1072                self.simplify_place_projection(place, location);
1073                return self.new_pointer(*place, AddressKind::Ref(borrow_kind));
1074            }
1075            Rvalue::RawPtr(mutbl, ref mut place) => {
1076                self.simplify_place_projection(place, location);
1077                return self.new_pointer(*place, AddressKind::Address(mutbl));
1078            }
1079            Rvalue::WrapUnsafeBinder(ref mut op, _) => {
1080                let value = self.simplify_operand(op, location)?;
1081                Value::Cast { kind: CastKind::Transmute, value }
1082            }
1083
1084            // Operations.
1085            Rvalue::Cast(ref mut kind, ref mut value, to) => {
1086                return self.simplify_cast(kind, value, to, location);
1087            }
1088            Rvalue::BinaryOp(op, box (ref mut lhs, ref mut rhs)) => {
1089                return self.simplify_binary(op, lhs, rhs, location);
1090            }
1091            Rvalue::UnaryOp(op, ref mut arg_op) => {
1092                return self.simplify_unary(op, arg_op, location);
1093            }
1094            Rvalue::Discriminant(ref mut place) => {
1095                let place = self.simplify_place_value(place, location)?;
1096                if let Some(discr) = self.simplify_discriminant(place) {
1097                    return Some(discr);
1098                }
1099                Value::Discriminant(place)
1100            }
1101
1102            // Unsupported values.
1103            Rvalue::ThreadLocalRef(..) => return None,
1104            Rvalue::CopyForDeref(_) => {
1105                bug!("forbidden in runtime MIR: {rvalue:?}")
1106            }
1107        };
1108        let ty = rvalue.ty(self.local_decls, self.tcx);
1109        Some(self.insert(ty, value))
1110    }
1111
1112    fn simplify_discriminant(&mut self, place: VnIndex) -> Option<VnIndex> {
1113        let enum_ty = self.ty(place);
1114        if enum_ty.is_enum()
1115            && let Value::Aggregate(variant, _) = self.get(place)
1116        {
1117            let discr = self.ecx.discriminant_for_variant(enum_ty, variant).discard_err()?;
1118            return Some(self.insert_scalar(discr.layout.ty, discr.to_scalar()));
1119        }
1120
1121        None
1122    }
1123
1124    fn try_as_place_elem(
1125        &mut self,
1126        ty: Ty<'tcx>,
1127        proj: ProjectionElem<VnIndex, ()>,
1128        loc: Location,
1129    ) -> Option<PlaceElem<'tcx>> {
1130        proj.try_map(
1131            |value| {
1132                let local = self.try_as_local(value, loc)?;
1133                self.reused_locals.insert(local);
1134                Some(local)
1135            },
1136            |()| ty,
1137        )
1138    }
1139
1140    fn simplify_aggregate_to_copy(
1141        &mut self,
1142        ty: Ty<'tcx>,
1143        variant_index: VariantIdx,
1144        fields: &[VnIndex],
1145    ) -> Option<VnIndex> {
1146        let Some(&first_field) = fields.first() else { return None };
1147        let Value::Projection(copy_from_value, _) = self.get(first_field) else { return None };
1148
1149        // All fields must correspond one-to-one and come from the same aggregate value.
1150        if fields.iter().enumerate().any(|(index, &v)| {
1151            if let Value::Projection(pointer, ProjectionElem::Field(from_index, _)) = self.get(v)
1152                && copy_from_value == pointer
1153                && from_index.index() == index
1154            {
1155                return false;
1156            }
1157            true
1158        }) {
1159            return None;
1160        }
1161
1162        let mut copy_from_local_value = copy_from_value;
1163        if let Value::Projection(pointer, proj) = self.get(copy_from_value)
1164            && let ProjectionElem::Downcast(_, read_variant) = proj
1165        {
1166            if variant_index == read_variant {
1167                // When copying a variant, there is no need to downcast.
1168                copy_from_local_value = pointer;
1169            } else {
1170                // The copied variant must be identical.
1171                return None;
1172            }
1173        }
1174
1175        // Both must be variants of the same type.
1176        if self.ty(copy_from_local_value) == ty { Some(copy_from_local_value) } else { None }
1177    }
1178
1179    fn simplify_aggregate(
1180        &mut self,
1181        rvalue: &mut Rvalue<'tcx>,
1182        location: Location,
1183    ) -> Option<VnIndex> {
1184        let tcx = self.tcx;
1185        let ty = rvalue.ty(self.local_decls, tcx);
1186
1187        let Rvalue::Aggregate(box ref kind, ref mut field_ops) = *rvalue else { bug!() };
1188
1189        if field_ops.is_empty() {
1190            let is_zst = match *kind {
1191                AggregateKind::Array(..)
1192                | AggregateKind::Tuple
1193                | AggregateKind::Closure(..)
1194                | AggregateKind::CoroutineClosure(..) => true,
1195                // Only enums can be non-ZST.
1196                AggregateKind::Adt(did, ..) => tcx.def_kind(did) != DefKind::Enum,
1197                // Coroutines are never ZST, as they at least contain the implicit states.
1198                AggregateKind::Coroutine(..) => false,
1199                AggregateKind::RawPtr(..) => bug!("MIR for RawPtr aggregate must have 2 fields"),
1200            };
1201
1202            if is_zst {
1203                return Some(self.insert_constant(Const::zero_sized(ty)));
1204            }
1205        }
1206
1207        let fields = self.arena.alloc_from_iter(field_ops.iter_mut().map(|op| {
1208            self.simplify_operand(op, location)
1209                .unwrap_or_else(|| self.new_opaque(op.ty(self.local_decls, self.tcx)))
1210        }));
1211
1212        let variant_index = match *kind {
1213            AggregateKind::Array(..) | AggregateKind::Tuple => {
1214                assert!(!field_ops.is_empty());
1215                FIRST_VARIANT
1216            }
1217            AggregateKind::Closure(..)
1218            | AggregateKind::CoroutineClosure(..)
1219            | AggregateKind::Coroutine(..) => FIRST_VARIANT,
1220            AggregateKind::Adt(_, variant_index, _, _, None) => variant_index,
1221            // Do not track unions.
1222            AggregateKind::Adt(_, _, _, _, Some(active_field)) => {
1223                let field = *fields.first()?;
1224                return Some(self.insert(ty, Value::Union(active_field, field)));
1225            }
1226            AggregateKind::RawPtr(..) => {
1227                assert_eq!(field_ops.len(), 2);
1228                let [mut pointer, metadata] = fields.try_into().unwrap();
1229
1230                // Any thin pointer of matching mutability is fine as the data pointer.
1231                let mut was_updated = false;
1232                while let Value::Cast { kind: CastKind::PtrToPtr, value: cast_value } =
1233                    self.get(pointer)
1234                    && let ty::RawPtr(from_pointee_ty, from_mtbl) = self.ty(cast_value).kind()
1235                    && let ty::RawPtr(_, output_mtbl) = ty.kind()
1236                    && from_mtbl == output_mtbl
1237                    && from_pointee_ty.is_sized(self.tcx, self.typing_env())
1238                {
1239                    pointer = cast_value;
1240                    was_updated = true;
1241                }
1242
1243                if was_updated && let Some(op) = self.try_as_operand(pointer, location) {
1244                    field_ops[FieldIdx::ZERO] = op;
1245                }
1246
1247                return Some(self.insert(ty, Value::RawPtr { pointer, metadata }));
1248            }
1249        };
1250
1251        if ty.is_array()
1252            && fields.len() > 4
1253            && let Ok(&first) = fields.iter().all_equal_value()
1254        {
1255            let len = ty::Const::from_target_usize(self.tcx, fields.len().try_into().unwrap());
1256            if let Some(op) = self.try_as_operand(first, location) {
1257                *rvalue = Rvalue::Repeat(op, len);
1258            }
1259            return Some(self.insert(ty, Value::Repeat(first, len)));
1260        }
1261
1262        if let Some(value) = self.simplify_aggregate_to_copy(ty, variant_index, &fields) {
1263            if let Some(place) = self.try_as_place(value, location, true) {
1264                self.reused_locals.insert(place.local);
1265                *rvalue = Rvalue::Use(Operand::Copy(place));
1266            }
1267            return Some(value);
1268        }
1269
1270        Some(self.insert(ty, Value::Aggregate(variant_index, fields)))
1271    }
1272
1273    #[instrument(level = "trace", skip(self), ret)]
1274    fn simplify_unary(
1275        &mut self,
1276        op: UnOp,
1277        arg_op: &mut Operand<'tcx>,
1278        location: Location,
1279    ) -> Option<VnIndex> {
1280        let mut arg_index = self.simplify_operand(arg_op, location)?;
1281        let arg_ty = self.ty(arg_index);
1282        let ret_ty = op.ty(self.tcx, arg_ty);
1283
1284        // PtrMetadata doesn't care about *const vs *mut vs & vs &mut,
1285        // so start by removing those distinctions so we can update the `Operand`
1286        if op == UnOp::PtrMetadata {
1287            let mut was_updated = false;
1288            loop {
1289                arg_index = match self.get(arg_index) {
1290                    // Pointer casts that preserve metadata, such as
1291                    // `*const [i32]` <-> `*mut [i32]` <-> `*mut [f32]`.
1292                    // It's critical that this not eliminate cases like
1293                    // `*const [T]` -> `*const T` which remove metadata.
1294                    // We run on potentially-generic MIR, though, so unlike codegen
1295                    // we can't always know exactly what the metadata are.
1296                    // To allow things like `*mut (?A, ?T)` <-> `*mut (?B, ?T)`,
1297                    // it's fine to get a projection as the type.
1298                    Value::Cast { kind: CastKind::PtrToPtr, value: inner }
1299                        if self.pointers_have_same_metadata(self.ty(inner), arg_ty) =>
1300                    {
1301                        inner
1302                    }
1303
1304                    // We have an unsizing cast, which assigns the length to wide pointer metadata.
1305                    Value::Cast {
1306                        kind: CastKind::PointerCoercion(ty::adjustment::PointerCoercion::Unsize, _),
1307                        value: from,
1308                    } if let Some(from) = self.ty(from).builtin_deref(true)
1309                        && let ty::Array(_, len) = from.kind()
1310                        && let Some(to) = self.ty(arg_index).builtin_deref(true)
1311                        && let ty::Slice(..) = to.kind() =>
1312                    {
1313                        return Some(self.insert_constant(Const::Ty(self.tcx.types.usize, *len)));
1314                    }
1315
1316                    // `&mut *p`, `&raw *p`, etc don't change metadata.
1317                    Value::Address { base: AddressBase::Deref(reborrowed), projection, .. }
1318                        if projection.is_empty() =>
1319                    {
1320                        reborrowed
1321                    }
1322
1323                    _ => break,
1324                };
1325                was_updated = true;
1326            }
1327
1328            if was_updated && let Some(op) = self.try_as_operand(arg_index, location) {
1329                *arg_op = op;
1330            }
1331        }
1332
1333        let value = match (op, self.get(arg_index)) {
1334            (UnOp::Not, Value::UnaryOp(UnOp::Not, inner)) => return Some(inner),
1335            (UnOp::Neg, Value::UnaryOp(UnOp::Neg, inner)) => return Some(inner),
1336            (UnOp::Not, Value::BinaryOp(BinOp::Eq, lhs, rhs)) => {
1337                Value::BinaryOp(BinOp::Ne, lhs, rhs)
1338            }
1339            (UnOp::Not, Value::BinaryOp(BinOp::Ne, lhs, rhs)) => {
1340                Value::BinaryOp(BinOp::Eq, lhs, rhs)
1341            }
1342            (UnOp::PtrMetadata, Value::RawPtr { metadata, .. }) => return Some(metadata),
1343            // We have an unsizing cast, which assigns the length to wide pointer metadata.
1344            (
1345                UnOp::PtrMetadata,
1346                Value::Cast {
1347                    kind: CastKind::PointerCoercion(ty::adjustment::PointerCoercion::Unsize, _),
1348                    value: inner,
1349                },
1350            ) if let ty::Slice(..) = arg_ty.builtin_deref(true).unwrap().kind()
1351                && let ty::Array(_, len) = self.ty(inner).builtin_deref(true).unwrap().kind() =>
1352            {
1353                return Some(self.insert_constant(Const::Ty(self.tcx.types.usize, *len)));
1354            }
1355            _ => Value::UnaryOp(op, arg_index),
1356        };
1357        Some(self.insert(ret_ty, value))
1358    }
1359
1360    #[instrument(level = "trace", skip(self), ret)]
1361    fn simplify_binary(
1362        &mut self,
1363        op: BinOp,
1364        lhs_operand: &mut Operand<'tcx>,
1365        rhs_operand: &mut Operand<'tcx>,
1366        location: Location,
1367    ) -> Option<VnIndex> {
1368        let lhs = self.simplify_operand(lhs_operand, location);
1369        let rhs = self.simplify_operand(rhs_operand, location);
1370
1371        // Only short-circuit options after we called `simplify_operand`
1372        // on both operands for side effect.
1373        let mut lhs = lhs?;
1374        let mut rhs = rhs?;
1375
1376        let lhs_ty = self.ty(lhs);
1377
1378        // If we're comparing pointers, remove `PtrToPtr` casts if the from
1379        // types of both casts and the metadata all match.
1380        if let BinOp::Eq | BinOp::Ne | BinOp::Lt | BinOp::Le | BinOp::Gt | BinOp::Ge = op
1381            && lhs_ty.is_any_ptr()
1382            && let Value::Cast { kind: CastKind::PtrToPtr, value: lhs_value } = self.get(lhs)
1383            && let Value::Cast { kind: CastKind::PtrToPtr, value: rhs_value } = self.get(rhs)
1384            && let lhs_from = self.ty(lhs_value)
1385            && lhs_from == self.ty(rhs_value)
1386            && self.pointers_have_same_metadata(lhs_from, lhs_ty)
1387        {
1388            lhs = lhs_value;
1389            rhs = rhs_value;
1390            if let Some(lhs_op) = self.try_as_operand(lhs, location)
1391                && let Some(rhs_op) = self.try_as_operand(rhs, location)
1392            {
1393                *lhs_operand = lhs_op;
1394                *rhs_operand = rhs_op;
1395            }
1396        }
1397
1398        if let Some(value) = self.simplify_binary_inner(op, lhs_ty, lhs, rhs) {
1399            return Some(value);
1400        }
1401        let ty = op.ty(self.tcx, lhs_ty, self.ty(rhs));
1402        let value = Value::BinaryOp(op, lhs, rhs);
1403        Some(self.insert(ty, value))
1404    }
1405
1406    fn simplify_binary_inner(
1407        &mut self,
1408        op: BinOp,
1409        lhs_ty: Ty<'tcx>,
1410        lhs: VnIndex,
1411        rhs: VnIndex,
1412    ) -> Option<VnIndex> {
1413        // Floats are weird enough that none of the logic below applies.
1414        let reasonable_ty =
1415            lhs_ty.is_integral() || lhs_ty.is_bool() || lhs_ty.is_char() || lhs_ty.is_any_ptr();
1416        if !reasonable_ty {
1417            return None;
1418        }
1419
1420        let layout = self.ecx.layout_of(lhs_ty).ok()?;
1421
1422        let mut as_bits = |value: VnIndex| {
1423            let constant = self.eval_to_const(value)?;
1424            if layout.backend_repr.is_scalar() {
1425                let scalar = self.ecx.read_scalar(constant).discard_err()?;
1426                scalar.to_bits(constant.layout.size).discard_err()
1427            } else {
1428                // `constant` is a wide pointer. Do not evaluate to bits.
1429                None
1430            }
1431        };
1432
1433        // Represent the values as `Left(bits)` or `Right(VnIndex)`.
1434        use Either::{Left, Right};
1435        let a = as_bits(lhs).map_or(Right(lhs), Left);
1436        let b = as_bits(rhs).map_or(Right(rhs), Left);
1437
1438        let result = match (op, a, b) {
1439            // Neutral elements.
1440            (
1441                BinOp::Add
1442                | BinOp::AddWithOverflow
1443                | BinOp::AddUnchecked
1444                | BinOp::BitOr
1445                | BinOp::BitXor,
1446                Left(0),
1447                Right(p),
1448            )
1449            | (
1450                BinOp::Add
1451                | BinOp::AddWithOverflow
1452                | BinOp::AddUnchecked
1453                | BinOp::BitOr
1454                | BinOp::BitXor
1455                | BinOp::Sub
1456                | BinOp::SubWithOverflow
1457                | BinOp::SubUnchecked
1458                | BinOp::Offset
1459                | BinOp::Shl
1460                | BinOp::Shr,
1461                Right(p),
1462                Left(0),
1463            )
1464            | (BinOp::Mul | BinOp::MulWithOverflow | BinOp::MulUnchecked, Left(1), Right(p))
1465            | (
1466                BinOp::Mul | BinOp::MulWithOverflow | BinOp::MulUnchecked | BinOp::Div,
1467                Right(p),
1468                Left(1),
1469            ) => p,
1470            // Attempt to simplify `x & ALL_ONES` to `x`, with `ALL_ONES` depending on type size.
1471            (BinOp::BitAnd, Right(p), Left(ones)) | (BinOp::BitAnd, Left(ones), Right(p))
1472                if ones == layout.size.truncate(u128::MAX)
1473                    || (layout.ty.is_bool() && ones == 1) =>
1474            {
1475                p
1476            }
1477            // Absorbing elements.
1478            (
1479                BinOp::Mul | BinOp::MulWithOverflow | BinOp::MulUnchecked | BinOp::BitAnd,
1480                _,
1481                Left(0),
1482            )
1483            | (BinOp::Rem, _, Left(1))
1484            | (
1485                BinOp::Mul
1486                | BinOp::MulWithOverflow
1487                | BinOp::MulUnchecked
1488                | BinOp::Div
1489                | BinOp::Rem
1490                | BinOp::BitAnd
1491                | BinOp::Shl
1492                | BinOp::Shr,
1493                Left(0),
1494                _,
1495            ) => self.insert_scalar(lhs_ty, Scalar::from_uint(0u128, layout.size)),
1496            // Attempt to simplify `x | ALL_ONES` to `ALL_ONES`.
1497            (BinOp::BitOr, _, Left(ones)) | (BinOp::BitOr, Left(ones), _)
1498                if ones == layout.size.truncate(u128::MAX)
1499                    || (layout.ty.is_bool() && ones == 1) =>
1500            {
1501                self.insert_scalar(lhs_ty, Scalar::from_uint(ones, layout.size))
1502            }
1503            // Sub/Xor with itself.
1504            (BinOp::Sub | BinOp::SubWithOverflow | BinOp::SubUnchecked | BinOp::BitXor, a, b)
1505                if a == b =>
1506            {
1507                self.insert_scalar(lhs_ty, Scalar::from_uint(0u128, layout.size))
1508            }
1509            // Comparison:
1510            // - if both operands can be computed as bits, just compare the bits;
1511            // - if we proved that both operands have the same value, we can insert true/false;
1512            // - otherwise, do nothing, as we do not try to prove inequality.
1513            (BinOp::Eq, Left(a), Left(b)) => self.insert_bool(a == b),
1514            (BinOp::Eq, a, b) if a == b => self.insert_bool(true),
1515            (BinOp::Ne, Left(a), Left(b)) => self.insert_bool(a != b),
1516            (BinOp::Ne, a, b) if a == b => self.insert_bool(false),
1517            _ => return None,
1518        };
1519
1520        if op.is_overflowing() {
1521            let ty = Ty::new_tup(self.tcx, &[self.ty(result), self.tcx.types.bool]);
1522            let false_val = self.insert_bool(false);
1523            Some(self.insert_tuple(ty, &[result, false_val]))
1524        } else {
1525            Some(result)
1526        }
1527    }
1528
1529    fn simplify_cast(
1530        &mut self,
1531        initial_kind: &mut CastKind,
1532        initial_operand: &mut Operand<'tcx>,
1533        to: Ty<'tcx>,
1534        location: Location,
1535    ) -> Option<VnIndex> {
1536        use CastKind::*;
1537        use rustc_middle::ty::adjustment::PointerCoercion::*;
1538
1539        let mut kind = *initial_kind;
1540        let mut value = self.simplify_operand(initial_operand, location)?;
1541        let mut from = self.ty(value);
1542        if from == to {
1543            return Some(value);
1544        }
1545
1546        if let CastKind::PointerCoercion(ReifyFnPointer(_) | ClosureFnPointer(_), _) = kind {
1547            // Each reification of a generic fn may get a different pointer.
1548            // Do not try to merge them.
1549            return Some(self.new_opaque(to));
1550        }
1551
1552        let mut was_ever_updated = false;
1553        loop {
1554            let mut was_updated_this_iteration = false;
1555
1556            // Transmuting between raw pointers is just a pointer cast so long as
1557            // they have the same metadata type (like `*const i32` <=> `*mut u64`
1558            // or `*mut [i32]` <=> `*const [u64]`), including the common special
1559            // case of `*const T` <=> `*mut T`.
1560            if let Transmute = kind
1561                && from.is_raw_ptr()
1562                && to.is_raw_ptr()
1563                && self.pointers_have_same_metadata(from, to)
1564            {
1565                kind = PtrToPtr;
1566                was_updated_this_iteration = true;
1567            }
1568
1569            // If a cast just casts away the metadata again, then we can get it by
1570            // casting the original thin pointer passed to `from_raw_parts`
1571            if let PtrToPtr = kind
1572                && let Value::RawPtr { pointer, .. } = self.get(value)
1573                && let ty::RawPtr(to_pointee, _) = to.kind()
1574                && to_pointee.is_sized(self.tcx, self.typing_env())
1575            {
1576                from = self.ty(pointer);
1577                value = pointer;
1578                was_updated_this_iteration = true;
1579                if from == to {
1580                    return Some(pointer);
1581                }
1582            }
1583
1584            // Aggregate-then-Transmute can just transmute the original field value,
1585            // so long as the bytes of a value from only from a single field.
1586            if let Transmute = kind
1587                && let Value::Aggregate(variant_idx, field_values) = self.get(value)
1588                && let Some((field_idx, field_ty)) =
1589                    self.value_is_all_in_one_field(from, variant_idx)
1590            {
1591                from = field_ty;
1592                value = field_values[field_idx.as_usize()];
1593                was_updated_this_iteration = true;
1594                if field_ty == to {
1595                    return Some(value);
1596                }
1597            }
1598
1599            // Various cast-then-cast cases can be simplified.
1600            if let Value::Cast { kind: inner_kind, value: inner_value } = self.get(value) {
1601                let inner_from = self.ty(inner_value);
1602                let new_kind = match (inner_kind, kind) {
1603                    // Even if there's a narrowing cast in here that's fine, because
1604                    // things like `*mut [i32] -> *mut i32 -> *const i32` and
1605                    // `*mut [i32] -> *const [i32] -> *const i32` can skip the middle in MIR.
1606                    (PtrToPtr, PtrToPtr) => Some(PtrToPtr),
1607                    // PtrToPtr-then-Transmute is fine so long as the pointer cast is identity:
1608                    // `*const T -> *mut T -> NonNull<T>` is fine, but we need to check for narrowing
1609                    // to skip things like `*const [i32] -> *const i32 -> NonNull<T>`.
1610                    (PtrToPtr, Transmute) if self.pointers_have_same_metadata(inner_from, from) => {
1611                        Some(Transmute)
1612                    }
1613                    // Similarly, for Transmute-then-PtrToPtr. Note that we need to check different
1614                    // variables for their metadata, and thus this can't merge with the previous arm.
1615                    (Transmute, PtrToPtr) if self.pointers_have_same_metadata(from, to) => {
1616                        Some(Transmute)
1617                    }
1618                    // It would be legal to always do this, but we don't want to hide information
1619                    // from the backend that it'd otherwise be able to use for optimizations.
1620                    (Transmute, Transmute)
1621                        if !self.transmute_may_have_niche_of_interest_to_backend(
1622                            inner_from, from, to,
1623                        ) =>
1624                    {
1625                        Some(Transmute)
1626                    }
1627                    _ => None,
1628                };
1629                if let Some(new_kind) = new_kind {
1630                    kind = new_kind;
1631                    from = inner_from;
1632                    value = inner_value;
1633                    was_updated_this_iteration = true;
1634                    if inner_from == to {
1635                        return Some(inner_value);
1636                    }
1637                }
1638            }
1639
1640            if was_updated_this_iteration {
1641                was_ever_updated = true;
1642            } else {
1643                break;
1644            }
1645        }
1646
1647        if was_ever_updated && let Some(op) = self.try_as_operand(value, location) {
1648            *initial_operand = op;
1649            *initial_kind = kind;
1650        }
1651
1652        Some(self.insert(to, Value::Cast { kind, value }))
1653    }
1654
1655    fn pointers_have_same_metadata(&self, left_ptr_ty: Ty<'tcx>, right_ptr_ty: Ty<'tcx>) -> bool {
1656        let left_meta_ty = left_ptr_ty.pointee_metadata_ty_or_projection(self.tcx);
1657        let right_meta_ty = right_ptr_ty.pointee_metadata_ty_or_projection(self.tcx);
1658        if left_meta_ty == right_meta_ty {
1659            true
1660        } else if let Ok(left) =
1661            self.tcx.try_normalize_erasing_regions(self.typing_env(), left_meta_ty)
1662            && let Ok(right) =
1663                self.tcx.try_normalize_erasing_regions(self.typing_env(), right_meta_ty)
1664        {
1665            left == right
1666        } else {
1667            false
1668        }
1669    }
1670
1671    /// Returns `false` if we're confident that the middle type doesn't have an
1672    /// interesting niche so we can skip that step when transmuting.
1673    ///
1674    /// The backend will emit `assume`s when transmuting between types with niches,
1675    /// so we want to preserve `i32 -> char -> u32` so that that data is around,
1676    /// but it's fine to skip whole-range-is-value steps like `A -> u32 -> B`.
1677    fn transmute_may_have_niche_of_interest_to_backend(
1678        &self,
1679        from_ty: Ty<'tcx>,
1680        middle_ty: Ty<'tcx>,
1681        to_ty: Ty<'tcx>,
1682    ) -> bool {
1683        let Ok(middle_layout) = self.ecx.layout_of(middle_ty) else {
1684            // If it's too generic or something, then assume it might be interesting later.
1685            return true;
1686        };
1687
1688        if middle_layout.uninhabited {
1689            return true;
1690        }
1691
1692        match middle_layout.backend_repr {
1693            BackendRepr::Scalar(mid) => {
1694                if mid.is_always_valid(&self.ecx) {
1695                    // With no niche it's never interesting, so don't bother
1696                    // looking at the layout of the other two types.
1697                    false
1698                } else if let Ok(from_layout) = self.ecx.layout_of(from_ty)
1699                    && !from_layout.uninhabited
1700                    && from_layout.size == middle_layout.size
1701                    && let BackendRepr::Scalar(from_a) = from_layout.backend_repr
1702                    && let mid_range = mid.valid_range(&self.ecx)
1703                    && let from_range = from_a.valid_range(&self.ecx)
1704                    && mid_range.contains_range(from_range, middle_layout.size)
1705                {
1706                    // The `from_range` is a (non-strict) subset of `mid_range`
1707                    // such as if we're doing `bool` -> `ascii::Char` -> `_`,
1708                    // where `from_range: 0..=1` and `mid_range: 0..=127`,
1709                    // and thus the middle doesn't tell us anything we don't
1710                    // already know from the initial type.
1711                    false
1712                } else if let Ok(to_layout) = self.ecx.layout_of(to_ty)
1713                    && !to_layout.uninhabited
1714                    && to_layout.size == middle_layout.size
1715                    && let BackendRepr::Scalar(to_a) = to_layout.backend_repr
1716                    && let mid_range = mid.valid_range(&self.ecx)
1717                    && let to_range = to_a.valid_range(&self.ecx)
1718                    && mid_range.contains_range(to_range, middle_layout.size)
1719                {
1720                    // The `to_range` is a (non-strict) subset of `mid_range`
1721                    // such as if we're doing `_` -> `ascii::Char` -> `bool`,
1722                    // where `mid_range: 0..=127` and `to_range: 0..=1`,
1723                    // and thus the middle doesn't tell us anything we don't
1724                    // already know from the final type.
1725                    false
1726                } else {
1727                    true
1728                }
1729            }
1730            BackendRepr::ScalarPair(a, b) => {
1731                !a.is_always_valid(&self.ecx) || !b.is_always_valid(&self.ecx)
1732            }
1733            BackendRepr::SimdVector { .. }
1734            | BackendRepr::SimdScalableVector { .. }
1735            | BackendRepr::Memory { .. } => false,
1736        }
1737    }
1738
1739    fn value_is_all_in_one_field(
1740        &self,
1741        ty: Ty<'tcx>,
1742        variant: VariantIdx,
1743    ) -> Option<(FieldIdx, Ty<'tcx>)> {
1744        if let Ok(layout) = self.ecx.layout_of(ty)
1745            && let abi::Variants::Single { index } = layout.variants
1746            && index == variant
1747            && let Some((field_idx, field_layout)) = layout.non_1zst_field(&self.ecx)
1748            && layout.size == field_layout.size
1749        {
1750            // We needed to check the variant to avoid trying to read the tag
1751            // field from an enum where no fields have variants, since that tag
1752            // field isn't in the `Aggregate` from which we're getting values.
1753            Some((field_idx, field_layout.ty))
1754        } else if let ty::Adt(adt, args) = ty.kind()
1755            && adt.is_struct()
1756            && adt.repr().transparent()
1757            && let [single_field] = adt.non_enum_variant().fields.raw.as_slice()
1758        {
1759            Some((FieldIdx::ZERO, single_field.ty(self.tcx, args)))
1760        } else {
1761            None
1762        }
1763    }
1764}
1765
1766/// Return true if any evaluation of this constant in the same MIR body
1767/// always returns the same value, taking into account even pointer identity tests.
1768///
1769/// In other words, this answers: is "cloning" the `Const` ok?
1770///
1771/// This returns `false` for constants that synthesize new `AllocId` when they are instantiated.
1772/// It is `true` for anything else, since a given `AllocId` *does* have a unique runtime value
1773/// within the scope of a single MIR body.
1774fn is_deterministic(c: Const<'_>) -> bool {
1775    // Primitive types cannot contain provenance and always have the same value.
1776    if c.ty().is_primitive() {
1777        return true;
1778    }
1779
1780    match c {
1781        // Some constants may generate fresh allocations for pointers they contain,
1782        // so using the same constant twice can yield two different results.
1783        // Notably, valtrees purposefully generate new allocations.
1784        Const::Ty(..) => false,
1785        // We do not know the contents, so don't attempt to do anything clever.
1786        Const::Unevaluated(..) => false,
1787        // When an evaluated constant contains provenance, it is encoded as an `AllocId`.
1788        // Cloning the constant will reuse the same `AllocId`. If this is in the same MIR
1789        // body, this same `AllocId` will result in the same pointer in codegen.
1790        Const::Val(..) => true,
1791    }
1792}
1793
1794/// Check if a constant may contain provenance information.
1795/// Can return `true` even if there is no provenance.
1796fn may_have_provenance(tcx: TyCtxt<'_>, value: ConstValue, size: Size) -> bool {
1797    match value {
1798        ConstValue::ZeroSized | ConstValue::Scalar(Scalar::Int(_)) => return false,
1799        ConstValue::Scalar(Scalar::Ptr(..)) | ConstValue::Slice { .. } => return true,
1800        ConstValue::Indirect { alloc_id, offset } => !tcx
1801            .global_alloc(alloc_id)
1802            .unwrap_memory()
1803            .inner()
1804            .provenance()
1805            .range_empty(AllocRange::from(offset..offset + size), &tcx),
1806    }
1807}
1808
1809fn op_to_prop_const<'tcx>(
1810    ecx: &mut InterpCx<'tcx, DummyMachine>,
1811    op: &OpTy<'tcx>,
1812) -> Option<ConstValue> {
1813    // Do not attempt to propagate unsized locals.
1814    if op.layout.is_unsized() {
1815        return None;
1816    }
1817
1818    // This constant is a ZST, just return an empty value.
1819    if op.layout.is_zst() {
1820        return Some(ConstValue::ZeroSized);
1821    }
1822
1823    // Do not synthetize too large constants. Codegen will just memcpy them, which we'd like to
1824    // avoid.
1825    // But we *do* want to synthesize any size constant if it is entirely uninit because that
1826    // benefits codegen, which has special handling for them.
1827    if !op.is_immediate_uninit()
1828        && !matches!(op.layout.backend_repr, BackendRepr::Scalar(..) | BackendRepr::ScalarPair(..))
1829    {
1830        return None;
1831    }
1832
1833    // If this constant has scalar ABI, return it as a `ConstValue::Scalar`.
1834    if let BackendRepr::Scalar(abi::Scalar::Initialized { .. }) = op.layout.backend_repr
1835        && let Some(scalar) = ecx.read_scalar(op).discard_err()
1836    {
1837        if !scalar.try_to_scalar_int().is_ok() {
1838            // Check that we do not leak a pointer.
1839            // Those pointers may lose part of their identity in codegen.
1840            // FIXME: remove this hack once https://github.com/rust-lang/rust/issues/128775 is fixed.
1841            return None;
1842        }
1843        return Some(ConstValue::Scalar(scalar));
1844    }
1845
1846    // If this constant is already represented as an `Allocation`,
1847    // try putting it into global memory to return it.
1848    if let Either::Left(mplace) = op.as_mplace_or_imm() {
1849        let (size, _align) = ecx.size_and_align_of_val(&mplace).discard_err()??;
1850
1851        // Do not try interning a value that contains provenance.
1852        // Due to https://github.com/rust-lang/rust/issues/128775, doing so could lead to bugs.
1853        // FIXME: remove this hack once that issue is fixed.
1854        let alloc_ref = ecx.get_ptr_alloc(mplace.ptr(), size).discard_err()??;
1855        if alloc_ref.has_provenance() {
1856            return None;
1857        }
1858
1859        let pointer = mplace.ptr().into_pointer_or_addr().ok()?;
1860        let (prov, offset) = pointer.prov_and_relative_offset();
1861        let alloc_id = prov.alloc_id();
1862        intern_const_alloc_for_constprop(ecx, alloc_id).discard_err()?;
1863
1864        // `alloc_id` may point to a static. Codegen will choke on an `Indirect` with anything
1865        // by `GlobalAlloc::Memory`, so do fall through to copying if needed.
1866        // FIXME: find a way to treat this more uniformly (probably by fixing codegen)
1867        if let GlobalAlloc::Memory(alloc) = ecx.tcx.global_alloc(alloc_id)
1868            // Transmuting a constant is just an offset in the allocation. If the alignment of the
1869            // allocation is not enough, fallback to copying into a properly aligned value.
1870            && alloc.inner().align >= op.layout.align.abi
1871        {
1872            return Some(ConstValue::Indirect { alloc_id, offset });
1873        }
1874    }
1875
1876    // Everything failed: create a new allocation to hold the data.
1877    let alloc_id =
1878        ecx.intern_with_temp_alloc(op.layout, |ecx, dest| ecx.copy_op(op, dest)).discard_err()?;
1879    Some(ConstValue::Indirect { alloc_id, offset: Size::ZERO })
1880}
1881
1882impl<'tcx> VnState<'_, '_, 'tcx> {
1883    /// If either [`Self::try_as_constant`] as [`Self::try_as_place`] succeeds,
1884    /// returns that result as an [`Operand`].
1885    fn try_as_operand(&mut self, index: VnIndex, location: Location) -> Option<Operand<'tcx>> {
1886        if let Some(const_) = self.try_as_constant(index) {
1887            Some(Operand::Constant(Box::new(const_)))
1888        } else if let Value::RuntimeChecks(c) = self.get(index) {
1889            Some(Operand::RuntimeChecks(c))
1890        } else if let Some(place) = self.try_as_place(index, location, false) {
1891            self.reused_locals.insert(place.local);
1892            Some(Operand::Copy(place))
1893        } else {
1894            None
1895        }
1896    }
1897
1898    /// If `index` is a `Value::Constant`, return the `Constant` to be put in the MIR.
1899    fn try_as_constant(&mut self, index: VnIndex) -> Option<ConstOperand<'tcx>> {
1900        let value = self.get(index);
1901
1902        // This was already an *evaluated* constant in MIR, do not change it.
1903        if let Value::Constant { value, disambiguator: None } = value
1904            && let Const::Val(..) = value
1905        {
1906            return Some(ConstOperand { span: DUMMY_SP, user_ty: None, const_: value });
1907        }
1908
1909        if let Some(value) = self.try_as_evaluated_constant(index) {
1910            return Some(ConstOperand { span: DUMMY_SP, user_ty: None, const_: value });
1911        }
1912
1913        // We failed to provide an evaluated form, fallback to using the unevaluated constant.
1914        if let Value::Constant { value, disambiguator: None } = value {
1915            return Some(ConstOperand { span: DUMMY_SP, user_ty: None, const_: value });
1916        }
1917
1918        None
1919    }
1920
1921    fn try_as_evaluated_constant(&mut self, index: VnIndex) -> Option<Const<'tcx>> {
1922        let op = self.eval_to_const(index)?;
1923        if op.layout.is_unsized() {
1924            // Do not attempt to propagate unsized locals.
1925            return None;
1926        }
1927
1928        let value = op_to_prop_const(&mut self.ecx, op)?;
1929
1930        // Check that we do not leak a pointer.
1931        // Those pointers may lose part of their identity in codegen.
1932        // FIXME: remove this hack once https://github.com/rust-lang/rust/issues/128775 is fixed.
1933        if may_have_provenance(self.tcx, value, op.layout.size) {
1934            return None;
1935        }
1936
1937        Some(Const::Val(value, op.layout.ty))
1938    }
1939
1940    /// Construct a place which holds the same value as `index` and for which all locals strictly
1941    /// dominate `loc`. If you used this place, add its base local to `reused_locals` to remove
1942    /// storage statements.
1943    #[instrument(level = "trace", skip(self), ret)]
1944    fn try_as_place(
1945        &mut self,
1946        mut index: VnIndex,
1947        loc: Location,
1948        allow_complex_projection: bool,
1949    ) -> Option<Place<'tcx>> {
1950        let mut projection = SmallVec::<[PlaceElem<'tcx>; 1]>::new();
1951        loop {
1952            if let Some(local) = self.try_as_local(index, loc) {
1953                projection.reverse();
1954                let place =
1955                    Place { local, projection: self.tcx.mk_place_elems(projection.as_slice()) };
1956                return Some(place);
1957            } else if projection.last() == Some(&PlaceElem::Deref) {
1958                // `Deref` can only be the first projection in a place.
1959                // If we are here, we failed to find a local, and we already have a `Deref`.
1960                // Trying to add projections will only result in an ill-formed place.
1961                return None;
1962            } else if let Value::Projection(pointer, proj) = self.get(index)
1963                && (allow_complex_projection || proj.is_stable_offset())
1964                && let Some(proj) = self.try_as_place_elem(self.ty(index), proj, loc)
1965            {
1966                if proj == PlaceElem::Deref {
1967                    // We can introduce a new dereference if the source value cannot be changed in the body.
1968                    // Dereferencing an immutable argument always gives the same value in the body.
1969                    match self.get(pointer) {
1970                        Value::Argument(_)
1971                            if let Some(Mutability::Not) = self.ty(pointer).ref_mutability() => {}
1972                        _ => {
1973                            return None;
1974                        }
1975                    }
1976                }
1977                projection.push(proj);
1978                index = pointer;
1979            } else {
1980                return None;
1981            }
1982        }
1983    }
1984
1985    /// If there is a local which is assigned `index`, and its assignment strictly dominates `loc`,
1986    /// return it. If you used this local, add it to `reused_locals` to remove storage statements.
1987    fn try_as_local(&mut self, index: VnIndex, loc: Location) -> Option<Local> {
1988        let other = self.rev_locals.get(index)?;
1989        other
1990            .iter()
1991            .find(|&&other| self.ssa.assignment_dominates(&self.dominators, other, loc))
1992            .copied()
1993    }
1994}
1995
1996impl<'tcx> MutVisitor<'tcx> for VnState<'_, '_, 'tcx> {
1997    fn tcx(&self) -> TyCtxt<'tcx> {
1998        self.tcx
1999    }
2000
2001    fn visit_place(&mut self, place: &mut Place<'tcx>, context: PlaceContext, location: Location) {
2002        self.simplify_place_projection(place, location);
2003        self.super_place(place, context, location);
2004    }
2005
2006    fn visit_operand(&mut self, operand: &mut Operand<'tcx>, location: Location) {
2007        self.simplify_operand(operand, location);
2008        self.super_operand(operand, location);
2009    }
2010
2011    fn visit_assign(
2012        &mut self,
2013        lhs: &mut Place<'tcx>,
2014        rvalue: &mut Rvalue<'tcx>,
2015        location: Location,
2016    ) {
2017        self.simplify_place_projection(lhs, location);
2018
2019        let value = self.simplify_rvalue(lhs, rvalue, location);
2020        if let Some(value) = value {
2021            if let Some(const_) = self.try_as_constant(value) {
2022                *rvalue = Rvalue::Use(Operand::Constant(Box::new(const_)));
2023            } else if let Some(place) = self.try_as_place(value, location, false)
2024                && *rvalue != Rvalue::Use(Operand::Move(place))
2025                && *rvalue != Rvalue::Use(Operand::Copy(place))
2026            {
2027                *rvalue = Rvalue::Use(Operand::Copy(place));
2028                self.reused_locals.insert(place.local);
2029            }
2030        }
2031
2032        if let Some(local) = lhs.as_local()
2033            && self.ssa.is_ssa(local)
2034            && let rvalue_ty = rvalue.ty(self.local_decls, self.tcx)
2035            // FIXME(#112651) `rvalue` may have a subtype to `local`. We can only mark
2036            // `local` as reusable if we have an exact type match.
2037            && self.local_decls[local].ty == rvalue_ty
2038        {
2039            let value = value.unwrap_or_else(|| self.new_opaque(rvalue_ty));
2040            self.assign(local, value);
2041        }
2042    }
2043
2044    fn visit_terminator(&mut self, terminator: &mut Terminator<'tcx>, location: Location) {
2045        if let Terminator { kind: TerminatorKind::Call { destination, .. }, .. } = terminator {
2046            if let Some(local) = destination.as_local()
2047                && self.ssa.is_ssa(local)
2048            {
2049                let ty = self.local_decls[local].ty;
2050                let opaque = self.new_opaque(ty);
2051                self.assign(local, opaque);
2052            }
2053        }
2054        self.super_terminator(terminator, location);
2055    }
2056}
2057
2058struct StorageRemover<'tcx> {
2059    tcx: TyCtxt<'tcx>,
2060    reused_locals: DenseBitSet<Local>,
2061    storage_to_remove: DenseBitSet<Local>,
2062}
2063
2064impl<'tcx> MutVisitor<'tcx> for StorageRemover<'tcx> {
2065    fn tcx(&self) -> TyCtxt<'tcx> {
2066        self.tcx
2067    }
2068
2069    fn visit_operand(&mut self, operand: &mut Operand<'tcx>, _: Location) {
2070        if let Operand::Move(place) = *operand
2071            && !place.is_indirect_first_projection()
2072            && self.reused_locals.contains(place.local)
2073        {
2074            *operand = Operand::Copy(place);
2075        }
2076    }
2077
2078    fn visit_statement(&mut self, stmt: &mut Statement<'tcx>, loc: Location) {
2079        match stmt.kind {
2080            // When removing storage statements, we need to remove both (#107511).
2081            StatementKind::StorageLive(l) | StatementKind::StorageDead(l)
2082                if self.storage_to_remove.contains(l) =>
2083            {
2084                stmt.make_nop(true)
2085            }
2086            _ => self.super_statement(stmt, loc),
2087        }
2088    }
2089}
2090
2091struct StorageChecker<'a, 'tcx> {
2092    reused_locals: &'a DenseBitSet<Local>,
2093    storage_to_remove: DenseBitSet<Local>,
2094    maybe_uninit: ResultsCursor<'a, 'tcx, MaybeUninitializedLocals>,
2095}
2096
2097impl<'a, 'tcx> Visitor<'tcx> for StorageChecker<'a, 'tcx> {
2098    fn visit_local(&mut self, local: Local, context: PlaceContext, location: Location) {
2099        match context {
2100            // These mutating uses do not require the local to be initialized,
2101            // so we cannot use our maybe-uninit check on them.
2102            // However, GVN doesn't introduce or move mutations,
2103            // so this local must already have valid storage at this location.
2104            PlaceContext::MutatingUse(MutatingUseContext::AsmOutput)
2105            | PlaceContext::MutatingUse(MutatingUseContext::Call)
2106            | PlaceContext::MutatingUse(MutatingUseContext::Store)
2107            | PlaceContext::MutatingUse(MutatingUseContext::Yield)
2108            | PlaceContext::NonUse(_) => {
2109                return;
2110            }
2111            // Must check validity for other mutating usages and all non-mutating uses.
2112            PlaceContext::MutatingUse(_) | PlaceContext::NonMutatingUse(_) => {}
2113        }
2114
2115        // We only need to check reused locals which we haven't already removed storage for.
2116        if !self.reused_locals.contains(local) || self.storage_to_remove.contains(local) {
2117            return;
2118        }
2119
2120        self.maybe_uninit.seek_before_primary_effect(location);
2121
2122        if self.maybe_uninit.get().contains(local) {
2123            debug!(
2124                ?location,
2125                ?local,
2126                "local is reused and is maybe uninit at this location, marking it for storage statement removal"
2127            );
2128            self.storage_to_remove.insert(local);
2129        }
2130    }
2131}