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