1use std::borrow::Cow;
97use std::hash::{Hash, Hasher};
98
99use either::Either;
100use itertools::Itertools as _;
101use rustc_abi::{self as abi, BackendRepr, FIRST_VARIANT, FieldIdx, Primitive, Size, VariantIdx};
102use rustc_arena::DroplessArena;
103use rustc_const_eval::const_eval::DummyMachine;
104use rustc_const_eval::interpret::{
105 ImmTy, Immediate, InterpCx, MemPlaceMeta, MemoryKind, OpTy, Projectable, Scalar,
106 intern_const_alloc_for_constprop,
107};
108use rustc_data_structures::fx::FxHasher;
109use rustc_data_structures::graph::dominators::Dominators;
110use rustc_data_structures::hash_table::{Entry, HashTable};
111use rustc_hir::def::DefKind;
112use rustc_index::bit_set::DenseBitSet;
113use rustc_index::{IndexVec, newtype_index};
114use rustc_middle::bug;
115use rustc_middle::mir::interpret::{AllocRange, GlobalAlloc};
116use rustc_middle::mir::visit::*;
117use rustc_middle::mir::*;
118use rustc_middle::ty::layout::HasTypingEnv;
119use rustc_middle::ty::{self, Ty, TyCtxt};
120use rustc_mir_dataflow::{Analysis, ResultsCursor};
121use rustc_span::DUMMY_SP;
122use smallvec::SmallVec;
123use tracing::{debug, instrument, trace};
124
125use crate::ssa::{MaybeUninitializedLocals, SsaLocals};
126
127pub(super) struct GVN;
128
129impl<'tcx> crate::MirPass<'tcx> for GVN {
130 fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
131 sess.mir_opt_level() >= 2
132 }
133
134 #[instrument(level = "trace", skip(self, tcx, body))]
135 fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
136 debug!(def_id = ?body.source.def_id());
137
138 let typing_env = body.typing_env(tcx);
139 let ssa = SsaLocals::new(tcx, body, typing_env);
140 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 let storage_to_remove = if tcx.sess.emit_lifetime_markers() {
162 let maybe_uninit = MaybeUninitializedLocals
163 .iterate_to_fixpoint(tcx, body, Some("mir_opt::gvn"))
164 .into_results_cursor(body);
165
166 let mut storage_checker = StorageChecker {
167 reused_locals: &state.reused_locals,
168 storage_to_remove: DenseBitSet::new_empty(body.local_decls.len()),
169 maybe_uninit,
170 };
171
172 for (bb, data) in traversal::reachable(body) {
173 storage_checker.visit_basic_block_data(bb, data);
174 }
175
176 storage_checker.storage_to_remove
177 } else {
178 state.reused_locals.clone()
180 };
181
182 debug!(?storage_to_remove);
183
184 StorageRemover { tcx, reused_locals: state.reused_locals, storage_to_remove }
185 .visit_body_preserves_cfg(body);
186 }
187
188 fn is_required(&self) -> bool {
189 false
190 }
191}
192
193newtype_index! {
194 #[debug_format = "_v{}"]
196 struct VnIndex {}
197}
198
199#[derive(Copy, Clone, Debug, Eq)]
203struct VnOpaque;
204impl PartialEq for VnOpaque {
205 fn eq(&self, _: &VnOpaque) -> bool {
206 unreachable!()
208 }
209}
210impl Hash for VnOpaque {
211 fn hash<T: Hasher>(&self, _: &mut T) {
212 unreachable!()
214 }
215}
216
217#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
218enum AddressKind {
219 Ref(BorrowKind),
220 Address(RawPtrKind),
221}
222
223#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
224enum AddressBase {
225 Local(Local),
227 Deref(VnIndex),
229}
230
231#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
232enum Value<'a, 'tcx> {
233 Opaque(VnOpaque),
236 Argument(VnOpaque),
238 Constant {
240 value: Const<'tcx>,
241 disambiguator: Option<VnOpaque>,
245 },
246
247 Aggregate(VariantIdx, &'a [VnIndex]),
251 Union(FieldIdx, VnIndex),
253 RawPtr {
255 pointer: VnIndex,
257 metadata: VnIndex,
259 },
260 Repeat(VnIndex, ty::Const<'tcx>),
262 Address {
264 base: AddressBase,
265 projection: &'a [ProjectionElem<VnIndex, Ty<'tcx>>],
268 kind: AddressKind,
269 provenance: VnOpaque,
271 },
272
273 Projection(VnIndex, ProjectionElem<VnIndex, ()>),
276 Discriminant(VnIndex),
278
279 RuntimeChecks(RuntimeChecks),
281 UnaryOp(UnOp, VnIndex),
282 BinaryOp(BinOp, VnIndex, VnIndex),
283 Cast {
284 kind: CastKind,
285 value: VnIndex,
286 },
287}
288
289struct ValueSet<'a, 'tcx> {
295 indices: HashTable<VnIndex>,
296 hashes: IndexVec<VnIndex, u64>,
297 values: IndexVec<VnIndex, Value<'a, 'tcx>>,
298 types: IndexVec<VnIndex, Ty<'tcx>>,
299}
300
301impl<'a, 'tcx> ValueSet<'a, 'tcx> {
302 fn new(num_values: usize) -> ValueSet<'a, 'tcx> {
303 ValueSet {
304 indices: HashTable::with_capacity(num_values),
305 hashes: IndexVec::with_capacity(num_values),
306 values: IndexVec::with_capacity(num_values),
307 types: IndexVec::with_capacity(num_values),
308 }
309 }
310
311 #[inline]
314 fn insert_unique(
315 &mut self,
316 ty: Ty<'tcx>,
317 value: impl FnOnce(VnOpaque) -> Value<'a, 'tcx>,
318 ) -> VnIndex {
319 let value = value(VnOpaque);
320
321 debug_assert!(match value {
322 Value::Opaque(_) | Value::Argument(_) | Value::Address { .. } => true,
323 Value::Constant { disambiguator, .. } => disambiguator.is_some(),
324 _ => false,
325 });
326
327 let index = self.hashes.push(0);
328 let _index = self.types.push(ty);
329 debug_assert_eq!(index, _index);
330 let _index = self.values.push(value);
331 debug_assert_eq!(index, _index);
332 index
333 }
334
335 #[allow(rustc::disallowed_pass_by_ref)] fn insert(&mut self, ty: Ty<'tcx>, value: Value<'a, 'tcx>) -> (VnIndex, bool) {
339 debug_assert!(match value {
340 Value::Opaque(_) | Value::Address { .. } => false,
341 Value::Constant { disambiguator, .. } => disambiguator.is_none(),
342 _ => true,
343 });
344
345 let hash: u64 = {
346 let mut h = FxHasher::default();
347 value.hash(&mut h);
348 ty.hash(&mut h);
349 h.finish()
350 };
351
352 let eq = |index: &VnIndex| self.values[*index] == value && self.types[*index] == ty;
353 let hasher = |index: &VnIndex| self.hashes[*index];
354 match self.indices.entry(hash, eq, hasher) {
355 Entry::Occupied(entry) => {
356 let index = *entry.get();
357 (index, false)
358 }
359 Entry::Vacant(entry) => {
360 let index = self.hashes.push(hash);
361 entry.insert(index);
362 let _index = self.values.push(value);
363 debug_assert_eq!(index, _index);
364 let _index = self.types.push(ty);
365 debug_assert_eq!(index, _index);
366 (index, true)
367 }
368 }
369 }
370
371 #[inline]
373 fn value(&self, index: VnIndex) -> Value<'a, 'tcx> {
374 self.values[index]
375 }
376
377 #[inline]
379 fn ty(&self, index: VnIndex) -> Ty<'tcx> {
380 self.types[index]
381 }
382}
383
384struct VnState<'body, 'a, 'tcx> {
385 tcx: TyCtxt<'tcx>,
386 ecx: InterpCx<'tcx, DummyMachine>,
387 local_decls: &'body LocalDecls<'tcx>,
388 is_coroutine: bool,
389 locals: IndexVec<Local, Option<VnIndex>>,
391 rev_locals: IndexVec<VnIndex, SmallVec<[Local; 1]>>,
394 values: ValueSet<'a, 'tcx>,
395 evaluated: IndexVec<VnIndex, Option<Option<&'a OpTy<'tcx>>>>,
400 ssa: &'body SsaLocals,
401 dominators: Dominators<BasicBlock>,
402 reused_locals: DenseBitSet<Local>,
403 arena: &'a DroplessArena,
404}
405
406impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
407 fn new(
408 tcx: TyCtxt<'tcx>,
409 body: &Body<'tcx>,
410 typing_env: ty::TypingEnv<'tcx>,
411 ssa: &'body SsaLocals,
412 dominators: Dominators<BasicBlock>,
413 local_decls: &'body LocalDecls<'tcx>,
414 arena: &'a DroplessArena,
415 ) -> Self {
416 let num_values =
421 2 * body.basic_blocks.iter().map(|bbdata| bbdata.statements.len()).sum::<usize>()
422 + 4 * body.basic_blocks.len();
423 VnState {
424 tcx,
425 ecx: InterpCx::new(tcx, DUMMY_SP, typing_env, DummyMachine),
426 local_decls,
427 is_coroutine: body.coroutine.is_some(),
428 locals: IndexVec::from_elem(None, local_decls),
429 rev_locals: IndexVec::with_capacity(num_values),
430 values: ValueSet::new(num_values),
431 evaluated: IndexVec::with_capacity(num_values),
432 ssa,
433 dominators,
434 reused_locals: DenseBitSet::new_empty(local_decls.len()),
435 arena,
436 }
437 }
438
439 fn typing_env(&self) -> ty::TypingEnv<'tcx> {
440 self.ecx.typing_env()
441 }
442
443 fn insert_unique(
444 &mut self,
445 ty: Ty<'tcx>,
446 value: impl FnOnce(VnOpaque) -> Value<'a, 'tcx>,
447 ) -> VnIndex {
448 let index = self.values.insert_unique(ty, value);
449 let _index = self.evaluated.push(None);
450 debug_assert_eq!(index, _index);
451 let _index = self.rev_locals.push(SmallVec::new());
452 debug_assert_eq!(index, _index);
453 index
454 }
455
456 #[instrument(level = "trace", skip(self), ret)]
457 fn insert(&mut self, ty: Ty<'tcx>, value: Value<'a, 'tcx>) -> VnIndex {
458 let (index, new) = self.values.insert(ty, value);
459 if new {
460 let _index = self.evaluated.push(None);
462 debug_assert_eq!(index, _index);
463 let _index = self.rev_locals.push(SmallVec::new());
464 debug_assert_eq!(index, _index);
465 }
466 index
467 }
468
469 #[instrument(level = "trace", skip(self), ret)]
472 fn new_opaque(&mut self, ty: Ty<'tcx>) -> VnIndex {
473 let index = self.insert_unique(ty, Value::Opaque);
474 self.evaluated[index] = Some(None);
475 index
476 }
477
478 #[instrument(level = "trace", skip(self), ret)]
479 fn new_argument(&mut self, ty: Ty<'tcx>) -> VnIndex {
480 let index = self.insert_unique(ty, Value::Argument);
481 self.evaluated[index] = Some(None);
482 index
483 }
484
485 #[instrument(level = "trace", skip(self), ret)]
487 fn new_pointer(&mut self, place: Place<'tcx>, kind: AddressKind) -> Option<VnIndex> {
488 let pty = place.ty(self.local_decls, self.tcx).ty;
489 let ty = match kind {
490 AddressKind::Ref(bk) => {
491 Ty::new_ref(self.tcx, self.tcx.lifetimes.re_erased, pty, bk.to_mutbl_lossy())
492 }
493 AddressKind::Address(mutbl) => Ty::new_ptr(self.tcx, pty, mutbl.to_mutbl_lossy()),
494 };
495
496 let mut projection = place.projection.iter();
497 let base = if place.is_indirect_first_projection() {
498 let base = self.locals[place.local]?;
499 projection.next();
501 AddressBase::Deref(base)
502 } else if self.ssa.is_ssa(place.local) {
503 AddressBase::Local(place.local)
505 } else {
506 return None;
507 };
508 let projection =
510 projection.map(|proj| proj.try_map(|index| self.locals[index], |ty| ty).ok_or(()));
511 let projection = self.arena.try_alloc_from_iter(projection).ok()?;
512
513 let index = self.insert_unique(ty, |provenance| Value::Address {
514 base,
515 projection,
516 kind,
517 provenance,
518 });
519 Some(index)
520 }
521
522 #[instrument(level = "trace", skip(self), ret)]
523 fn insert_constant(&mut self, value: Const<'tcx>) -> VnIndex {
524 if is_deterministic(value) {
525 let constant = Value::Constant { value, disambiguator: None };
527 self.insert(value.ty(), constant)
528 } else {
529 self.insert_unique(value.ty(), |disambiguator| Value::Constant {
532 value,
533 disambiguator: Some(disambiguator),
534 })
535 }
536 }
537
538 #[inline]
539 fn get(&self, index: VnIndex) -> Value<'a, 'tcx> {
540 self.values.value(index)
541 }
542
543 #[inline]
544 fn ty(&self, index: VnIndex) -> Ty<'tcx> {
545 self.values.ty(index)
546 }
547
548 #[instrument(level = "trace", skip(self))]
550 fn assign(&mut self, local: Local, value: VnIndex) {
551 debug_assert!(self.ssa.is_ssa(local));
552 self.locals[local] = Some(value);
553 self.rev_locals[value].push(local);
554 }
555
556 fn insert_bool(&mut self, flag: bool) -> VnIndex {
557 let value = Const::from_bool(self.tcx, flag);
559 debug_assert!(is_deterministic(value));
560 self.insert(self.tcx.types.bool, Value::Constant { value, disambiguator: None })
561 }
562
563 fn insert_scalar(&mut self, ty: Ty<'tcx>, scalar: Scalar) -> VnIndex {
564 let value = Const::from_scalar(self.tcx, scalar, ty);
566 debug_assert!(is_deterministic(value));
567 self.insert(ty, Value::Constant { value, disambiguator: None })
568 }
569
570 fn insert_tuple(&mut self, ty: Ty<'tcx>, values: &[VnIndex]) -> VnIndex {
571 self.insert(ty, Value::Aggregate(VariantIdx::ZERO, self.arena.alloc_slice(values)))
572 }
573
574 #[instrument(level = "trace", skip(self), ret)]
575 fn eval_to_const_inner(&mut self, value: VnIndex) -> Option<OpTy<'tcx>> {
576 use Value::*;
577 let ty = self.ty(value);
578 let ty = if !self.is_coroutine || ty.is_scalar() {
580 self.ecx.layout_of(ty).ok()?
581 } else {
582 return None;
583 };
584 let op = match self.get(value) {
585 _ if ty.is_zst() => ImmTy::uninit(ty).into(),
586
587 Opaque(_) | Argument(_) => return None,
588 RuntimeChecks(..) => return None,
590
591 Repeat(value, _count) => {
596 let value = self.eval_to_const(value)?;
597 if value.is_immediate_uninit() {
598 ImmTy::uninit(ty).into()
599 } else {
600 return None;
601 }
602 }
603 Constant { ref value, disambiguator: _ } => {
604 self.ecx.eval_mir_constant(value, DUMMY_SP, None).discard_err()?
605 }
606 Aggregate(variant, ref fields) => {
607 let fields =
608 fields.iter().map(|&f| self.eval_to_const(f)).collect::<Option<Vec<_>>>()?;
609 let variant = if ty.ty.is_enum() { Some(variant) } else { None };
610 let (BackendRepr::Scalar(..) | BackendRepr::ScalarPair(..)) = ty.backend_repr
611 else {
612 return None;
613 };
614 let dest = self.ecx.allocate(ty, MemoryKind::Stack).discard_err()?;
615 let variant_dest = if let Some(variant) = variant {
616 self.ecx.project_downcast(&dest, variant).discard_err()?
617 } else {
618 dest.clone()
619 };
620 for (field_index, op) in fields.into_iter().enumerate() {
621 let field_dest = self
622 .ecx
623 .project_field(&variant_dest, FieldIdx::from_usize(field_index))
624 .discard_err()?;
625 self.ecx.copy_op(op, &field_dest).discard_err()?;
626 }
627 self.ecx
628 .write_discriminant(variant.unwrap_or(FIRST_VARIANT), &dest)
629 .discard_err()?;
630 self.ecx
631 .alloc_mark_immutable(dest.ptr().provenance.unwrap().alloc_id())
632 .discard_err()?;
633 dest.into()
634 }
635 Union(active_field, field) => {
636 let field = self.eval_to_const(field)?;
637 if field.layout.layout.is_zst() {
638 ImmTy::from_immediate(Immediate::Uninit, ty).into()
639 } else if matches!(
640 ty.backend_repr,
641 BackendRepr::Scalar(..) | BackendRepr::ScalarPair(..)
642 ) {
643 let dest = self.ecx.allocate(ty, MemoryKind::Stack).discard_err()?;
644 let field_dest = self.ecx.project_field(&dest, active_field).discard_err()?;
645 self.ecx.copy_op(field, &field_dest).discard_err()?;
646 self.ecx
647 .alloc_mark_immutable(dest.ptr().provenance.unwrap().alloc_id())
648 .discard_err()?;
649 dest.into()
650 } else {
651 return None;
652 }
653 }
654 RawPtr { pointer, metadata } => {
655 let pointer = self.eval_to_const(pointer)?;
656 let metadata = self.eval_to_const(metadata)?;
657
658 let data = self.ecx.read_pointer(pointer).discard_err()?;
660 let meta = if metadata.layout.is_zst() {
661 MemPlaceMeta::None
662 } else {
663 MemPlaceMeta::Meta(self.ecx.read_scalar(metadata).discard_err()?)
664 };
665 let ptr_imm = Immediate::new_pointer_with_meta(data, meta, &self.ecx);
666 ImmTy::from_immediate(ptr_imm, ty).into()
667 }
668
669 Projection(base, elem) => {
670 let base = self.eval_to_const(base)?;
671 let elem = elem.try_map(|_| None, |()| ty.ty)?;
674 self.ecx.project(base, elem).discard_err()?
675 }
676 Address { base, projection, .. } => {
677 debug_assert!(!projection.contains(&ProjectionElem::Deref));
678 let pointer = match base {
679 AddressBase::Deref(pointer) => self.eval_to_const(pointer)?,
680 AddressBase::Local(_) => return None,
682 };
683 let mut mplace = self.ecx.deref_pointer(pointer).discard_err()?;
684 for elem in projection {
685 let elem = elem.try_map(|_| None, |ty| ty)?;
688 mplace = self.ecx.project(&mplace, elem).discard_err()?;
689 }
690 let pointer = mplace.to_ref(&self.ecx);
691 ImmTy::from_immediate(pointer, ty).into()
692 }
693
694 Discriminant(base) => {
695 let base = self.eval_to_const(base)?;
696 let variant = self.ecx.read_discriminant(base).discard_err()?;
697 let discr_value =
698 self.ecx.discriminant_for_variant(base.layout.ty, variant).discard_err()?;
699 discr_value.into()
700 }
701 UnaryOp(un_op, operand) => {
702 let operand = self.eval_to_const(operand)?;
703 let operand = self.ecx.read_immediate(operand).discard_err()?;
704 let val = self.ecx.unary_op(un_op, &operand).discard_err()?;
705 val.into()
706 }
707 BinaryOp(bin_op, lhs, rhs) => {
708 let lhs = self.eval_to_const(lhs)?;
709 let rhs = self.eval_to_const(rhs)?;
710 let lhs = self.ecx.read_immediate(lhs).discard_err()?;
711 let rhs = self.ecx.read_immediate(rhs).discard_err()?;
712 let val = self.ecx.binary_op(bin_op, &lhs, &rhs).discard_err()?;
713 val.into()
714 }
715 Cast { kind, value } => match kind {
716 CastKind::IntToInt | CastKind::IntToFloat => {
717 let value = self.eval_to_const(value)?;
718 let value = self.ecx.read_immediate(value).discard_err()?;
719 let res = self.ecx.int_to_int_or_float(&value, ty).discard_err()?;
720 res.into()
721 }
722 CastKind::FloatToFloat | CastKind::FloatToInt => {
723 let value = self.eval_to_const(value)?;
724 let value = self.ecx.read_immediate(value).discard_err()?;
725 let res = self.ecx.float_to_float_or_int(&value, ty).discard_err()?;
726 res.into()
727 }
728 CastKind::Transmute | CastKind::Subtype => {
729 let value = self.eval_to_const(value)?;
730 if value.as_mplace_or_imm().is_right() {
735 let can_transmute = match (value.layout.backend_repr, ty.backend_repr) {
736 (BackendRepr::Scalar(s1), BackendRepr::Scalar(s2)) => {
737 s1.size(&self.ecx) == s2.size(&self.ecx)
738 && !matches!(s1.primitive(), Primitive::Pointer(..))
739 }
740 (BackendRepr::ScalarPair(a1, b1), BackendRepr::ScalarPair(a2, b2)) => {
741 a1.size(&self.ecx) == a2.size(&self.ecx)
742 && b1.size(&self.ecx) == b2.size(&self.ecx)
743 && b1.align(&self.ecx) == b2.align(&self.ecx)
745 && !matches!(a1.primitive(), Primitive::Pointer(..))
747 && !matches!(b1.primitive(), Primitive::Pointer(..))
748 }
749 _ => false,
750 };
751 if !can_transmute {
752 return None;
753 }
754 }
755 value.offset(Size::ZERO, ty, &self.ecx).discard_err()?
756 }
757 CastKind::PointerCoercion(ty::adjustment::PointerCoercion::Unsize, _) => {
758 let src = self.eval_to_const(value)?;
759 let dest = self.ecx.allocate(ty, MemoryKind::Stack).discard_err()?;
760 self.ecx.unsize_into(src, ty, &dest).discard_err()?;
761 self.ecx
762 .alloc_mark_immutable(dest.ptr().provenance.unwrap().alloc_id())
763 .discard_err()?;
764 dest.into()
765 }
766 CastKind::FnPtrToPtr | CastKind::PtrToPtr => {
767 let src = self.eval_to_const(value)?;
768 let src = self.ecx.read_immediate(src).discard_err()?;
769 let ret = self.ecx.ptr_to_ptr(&src, ty).discard_err()?;
770 ret.into()
771 }
772 CastKind::PointerCoercion(ty::adjustment::PointerCoercion::UnsafeFnPointer, _) => {
773 let src = self.eval_to_const(value)?;
774 let src = self.ecx.read_immediate(src).discard_err()?;
775 ImmTy::from_immediate(*src, ty).into()
776 }
777 _ => return None,
778 },
779 };
780 Some(op)
781 }
782
783 fn eval_to_const(&mut self, index: VnIndex) -> Option<&'a OpTy<'tcx>> {
784 if let Some(op) = self.evaluated[index] {
785 return op;
786 }
787 let op = self.eval_to_const_inner(index);
788 self.evaluated[index] = Some(self.arena.alloc(op).as_ref());
789 self.evaluated[index].unwrap()
790 }
791
792 #[instrument(level = "trace", skip(self), ret)]
794 fn dereference_address(
795 &mut self,
796 base: AddressBase,
797 projection: &[ProjectionElem<VnIndex, Ty<'tcx>>],
798 ) -> Option<VnIndex> {
799 let (mut place_ty, mut value) = match base {
800 AddressBase::Local(local) => {
802 let local = self.locals[local]?;
803 let place_ty = PlaceTy::from_ty(self.ty(local));
804 (place_ty, local)
805 }
806 AddressBase::Deref(reborrow) => {
808 let place_ty = PlaceTy::from_ty(self.ty(reborrow));
809 self.project(place_ty, reborrow, ProjectionElem::Deref)?
810 }
811 };
812 for &proj in projection {
813 (place_ty, value) = self.project(place_ty, value, proj)?;
814 }
815 Some(value)
816 }
817
818 #[instrument(level = "trace", skip(self), ret)]
819 fn project(
820 &mut self,
821 place_ty: PlaceTy<'tcx>,
822 value: VnIndex,
823 proj: ProjectionElem<VnIndex, Ty<'tcx>>,
824 ) -> Option<(PlaceTy<'tcx>, VnIndex)> {
825 let projection_ty = place_ty.projection_ty(self.tcx, proj);
826 let proj = match proj {
827 ProjectionElem::Deref => {
828 if let Some(Mutability::Not) = place_ty.ty.ref_mutability()
829 && projection_ty.ty.is_freeze(self.tcx, self.typing_env())
830 {
831 if let Value::Address { base, projection, .. } = self.get(value)
832 && let Some(value) = self.dereference_address(base, projection)
833 {
834 return Some((projection_ty, value));
835 }
836 if projection_ty.ty.is_ref() {
846 return None;
847 }
848
849 let deref = self
852 .insert(projection_ty.ty, Value::Projection(value, ProjectionElem::Deref));
853 return Some((projection_ty, deref));
854 } else {
855 return None;
856 }
857 }
858 ProjectionElem::Downcast(name, index) => ProjectionElem::Downcast(name, index),
859 ProjectionElem::Field(f, _) => match self.get(value) {
860 Value::Aggregate(_, fields) => return Some((projection_ty, fields[f.as_usize()])),
861 Value::Union(active, field) if active == f => return Some((projection_ty, field)),
862 Value::Projection(outer_value, ProjectionElem::Downcast(_, read_variant))
863 if let Value::Aggregate(written_variant, fields) = self.get(outer_value)
864 && written_variant == read_variant =>
880 {
881 return Some((projection_ty, fields[f.as_usize()]));
882 }
883 _ => ProjectionElem::Field(f, ()),
884 },
885 ProjectionElem::Index(idx) => {
886 if let Value::Repeat(inner, _) = self.get(value) {
887 return Some((projection_ty, inner));
888 }
889 ProjectionElem::Index(idx)
890 }
891 ProjectionElem::ConstantIndex { offset, min_length, from_end } => {
892 match self.get(value) {
893 Value::Repeat(inner, _) => {
894 return Some((projection_ty, inner));
895 }
896 Value::Aggregate(_, operands) => {
897 let offset = if from_end {
898 operands.len() - offset as usize
899 } else {
900 offset as usize
901 };
902 let value = operands.get(offset).copied()?;
903 return Some((projection_ty, value));
904 }
905 _ => {}
906 };
907 ProjectionElem::ConstantIndex { offset, min_length, from_end }
908 }
909 ProjectionElem::Subslice { from, to, from_end } => {
910 ProjectionElem::Subslice { from, to, from_end }
911 }
912 ProjectionElem::OpaqueCast(_) => ProjectionElem::OpaqueCast(()),
913 ProjectionElem::UnwrapUnsafeBinder(_) => ProjectionElem::UnwrapUnsafeBinder(()),
914 };
915
916 let value = self.insert(projection_ty.ty, Value::Projection(value, proj));
917 Some((projection_ty, value))
918 }
919
920 #[instrument(level = "trace", skip(self))]
922 fn simplify_place_projection(&mut self, place: &mut Place<'tcx>, location: Location) {
923 if place.is_indirect_first_projection()
926 && let Some(base) = self.locals[place.local]
927 && let Some(new_local) = self.try_as_local(base, location)
928 && place.local != new_local
929 {
930 place.local = new_local;
931 self.reused_locals.insert(new_local);
932 }
933
934 let mut projection = Cow::Borrowed(&place.projection[..]);
935
936 for i in 0..projection.len() {
937 let elem = projection[i];
938 if let ProjectionElem::Index(idx_local) = elem
939 && let Some(idx) = self.locals[idx_local]
940 {
941 if let Some(offset) = self.eval_to_const(idx)
942 && let Some(offset) = self.ecx.read_target_usize(offset).discard_err()
943 && let Some(min_length) = offset.checked_add(1)
944 {
945 projection.to_mut()[i] =
946 ProjectionElem::ConstantIndex { offset, min_length, from_end: false };
947 } else if let Some(new_idx_local) = self.try_as_local(idx, location)
948 && idx_local != new_idx_local
949 {
950 projection.to_mut()[i] = ProjectionElem::Index(new_idx_local);
951 self.reused_locals.insert(new_idx_local);
952 }
953 }
954 }
955
956 if Cow::is_owned(&projection) {
957 place.projection = self.tcx.mk_place_elems(&projection);
958 }
959
960 trace!(?place);
961 }
962
963 #[instrument(level = "trace", skip(self), ret)]
966 fn compute_place_value(
967 &mut self,
968 place: Place<'tcx>,
969 location: Location,
970 ) -> Result<VnIndex, PlaceRef<'tcx>> {
971 let mut place_ref = place.as_ref();
974
975 let Some(mut value) = self.locals[place.local] else { return Err(place_ref) };
977 let mut place_ty = PlaceTy::from_ty(self.local_decls[place.local].ty);
979 for (index, proj) in place.projection.iter().enumerate() {
980 if let Some(local) = self.try_as_local(value, location) {
981 place_ref = PlaceRef { local, projection: &place.projection[index..] };
985 }
986
987 let Some(proj) = proj.try_map(|value| self.locals[value], |ty| ty) else {
988 return Err(place_ref);
989 };
990 let Some(ty_and_value) = self.project(place_ty, value, proj) else {
991 return Err(place_ref);
992 };
993 (place_ty, value) = ty_and_value;
994 }
995
996 Ok(value)
997 }
998
999 #[instrument(level = "trace", skip(self), ret)]
1002 fn simplify_place_value(
1003 &mut self,
1004 place: &mut Place<'tcx>,
1005 location: Location,
1006 ) -> Option<VnIndex> {
1007 self.simplify_place_projection(place, location);
1008
1009 match self.compute_place_value(*place, location) {
1010 Ok(value) => {
1011 if let Some(new_place) = self.try_as_place(value, location, true)
1012 && (new_place.local != place.local
1013 || new_place.projection.len() < place.projection.len())
1014 {
1015 *place = new_place;
1016 self.reused_locals.insert(new_place.local);
1017 }
1018 Some(value)
1019 }
1020 Err(place_ref) => {
1021 if place_ref.local != place.local
1022 || place_ref.projection.len() < place.projection.len()
1023 {
1024 *place = place_ref.project_deeper(&[], self.tcx);
1026 self.reused_locals.insert(place_ref.local);
1027 }
1028 None
1029 }
1030 }
1031 }
1032
1033 #[instrument(level = "trace", skip(self), ret)]
1034 fn simplify_operand(
1035 &mut self,
1036 operand: &mut Operand<'tcx>,
1037 location: Location,
1038 ) -> Option<VnIndex> {
1039 let value = match *operand {
1040 Operand::RuntimeChecks(c) => self.insert(self.tcx.types.bool, Value::RuntimeChecks(c)),
1041 Operand::Constant(ref constant) => self.insert_constant(constant.const_),
1042 Operand::Copy(ref mut place) | Operand::Move(ref mut place) => {
1043 self.simplify_place_value(place, location)?
1044 }
1045 };
1046 if let Some(const_) = self.try_as_constant(value) {
1047 *operand = Operand::Constant(Box::new(const_));
1048 } else if let Value::RuntimeChecks(c) = self.get(value) {
1049 *operand = Operand::RuntimeChecks(c);
1050 }
1051 Some(value)
1052 }
1053
1054 #[instrument(level = "trace", skip(self), ret)]
1055 fn simplify_rvalue(
1056 &mut self,
1057 lhs: &Place<'tcx>,
1058 rvalue: &mut Rvalue<'tcx>,
1059 location: Location,
1060 ) -> Option<VnIndex> {
1061 let value = match *rvalue {
1062 Rvalue::Use(ref mut operand) => return self.simplify_operand(operand, location),
1064
1065 Rvalue::Repeat(ref mut op, amount) => {
1067 let op = self.simplify_operand(op, location)?;
1068 Value::Repeat(op, amount)
1069 }
1070 Rvalue::Aggregate(..) => return self.simplify_aggregate(rvalue, location),
1071 Rvalue::Ref(_, borrow_kind, ref mut place) => {
1072 self.simplify_place_projection(place, location);
1073 return self.new_pointer(*place, AddressKind::Ref(borrow_kind));
1074 }
1075 Rvalue::RawPtr(mutbl, ref mut place) => {
1076 self.simplify_place_projection(place, location);
1077 return self.new_pointer(*place, AddressKind::Address(mutbl));
1078 }
1079 Rvalue::WrapUnsafeBinder(ref mut op, _) => {
1080 let value = self.simplify_operand(op, location)?;
1081 Value::Cast { kind: CastKind::Transmute, value }
1082 }
1083
1084 Rvalue::Cast(ref mut kind, ref mut value, to) => {
1086 return self.simplify_cast(kind, value, to, location);
1087 }
1088 Rvalue::BinaryOp(op, box (ref mut lhs, ref mut rhs)) => {
1089 return self.simplify_binary(op, lhs, rhs, location);
1090 }
1091 Rvalue::UnaryOp(op, ref mut arg_op) => {
1092 return self.simplify_unary(op, arg_op, location);
1093 }
1094 Rvalue::Discriminant(ref mut place) => {
1095 let place = self.simplify_place_value(place, location)?;
1096 if let Some(discr) = self.simplify_discriminant(place) {
1097 return Some(discr);
1098 }
1099 Value::Discriminant(place)
1100 }
1101
1102 Rvalue::ThreadLocalRef(..) => return None,
1104 Rvalue::CopyForDeref(_) => {
1105 bug!("forbidden in runtime MIR: {rvalue:?}")
1106 }
1107 };
1108 let ty = rvalue.ty(self.local_decls, self.tcx);
1109 Some(self.insert(ty, value))
1110 }
1111
1112 fn simplify_discriminant(&mut self, place: VnIndex) -> Option<VnIndex> {
1113 let enum_ty = self.ty(place);
1114 if enum_ty.is_enum()
1115 && let Value::Aggregate(variant, _) = self.get(place)
1116 {
1117 let discr = self.ecx.discriminant_for_variant(enum_ty, variant).discard_err()?;
1118 return Some(self.insert_scalar(discr.layout.ty, discr.to_scalar()));
1119 }
1120
1121 None
1122 }
1123
1124 fn try_as_place_elem(
1125 &mut self,
1126 ty: Ty<'tcx>,
1127 proj: ProjectionElem<VnIndex, ()>,
1128 loc: Location,
1129 ) -> Option<PlaceElem<'tcx>> {
1130 proj.try_map(
1131 |value| {
1132 let local = self.try_as_local(value, loc)?;
1133 self.reused_locals.insert(local);
1134 Some(local)
1135 },
1136 |()| ty,
1137 )
1138 }
1139
1140 fn simplify_aggregate_to_copy(
1141 &mut self,
1142 ty: Ty<'tcx>,
1143 variant_index: VariantIdx,
1144 fields: &[VnIndex],
1145 ) -> Option<VnIndex> {
1146 let Some(&first_field) = fields.first() else { return None };
1147 let Value::Projection(copy_from_value, _) = self.get(first_field) else { return None };
1148
1149 if fields.iter().enumerate().any(|(index, &v)| {
1151 if let Value::Projection(pointer, ProjectionElem::Field(from_index, _)) = self.get(v)
1152 && copy_from_value == pointer
1153 && from_index.index() == index
1154 {
1155 return false;
1156 }
1157 true
1158 }) {
1159 return None;
1160 }
1161
1162 let mut copy_from_local_value = copy_from_value;
1163 if let Value::Projection(pointer, proj) = self.get(copy_from_value)
1164 && let ProjectionElem::Downcast(_, read_variant) = proj
1165 {
1166 if variant_index == read_variant {
1167 copy_from_local_value = pointer;
1169 } else {
1170 return None;
1172 }
1173 }
1174
1175 if self.ty(copy_from_local_value) == ty { Some(copy_from_local_value) } else { None }
1177 }
1178
1179 fn simplify_aggregate(
1180 &mut self,
1181 rvalue: &mut Rvalue<'tcx>,
1182 location: Location,
1183 ) -> Option<VnIndex> {
1184 let tcx = self.tcx;
1185 let ty = rvalue.ty(self.local_decls, tcx);
1186
1187 let Rvalue::Aggregate(box ref kind, ref mut field_ops) = *rvalue else { bug!() };
1188
1189 if field_ops.is_empty() {
1190 let is_zst = match *kind {
1191 AggregateKind::Array(..)
1192 | AggregateKind::Tuple
1193 | AggregateKind::Closure(..)
1194 | AggregateKind::CoroutineClosure(..) => true,
1195 AggregateKind::Adt(did, ..) => tcx.def_kind(did) != DefKind::Enum,
1197 AggregateKind::Coroutine(..) => false,
1199 AggregateKind::RawPtr(..) => bug!("MIR for RawPtr aggregate must have 2 fields"),
1200 };
1201
1202 if is_zst {
1203 return Some(self.insert_constant(Const::zero_sized(ty)));
1204 }
1205 }
1206
1207 let fields = self.arena.alloc_from_iter(field_ops.iter_mut().map(|op| {
1208 self.simplify_operand(op, location)
1209 .unwrap_or_else(|| self.new_opaque(op.ty(self.local_decls, self.tcx)))
1210 }));
1211
1212 let variant_index = match *kind {
1213 AggregateKind::Array(..) | AggregateKind::Tuple => {
1214 assert!(!field_ops.is_empty());
1215 FIRST_VARIANT
1216 }
1217 AggregateKind::Closure(..)
1218 | AggregateKind::CoroutineClosure(..)
1219 | AggregateKind::Coroutine(..) => FIRST_VARIANT,
1220 AggregateKind::Adt(_, variant_index, _, _, None) => variant_index,
1221 AggregateKind::Adt(_, _, _, _, Some(active_field)) => {
1223 let field = *fields.first()?;
1224 return Some(self.insert(ty, Value::Union(active_field, field)));
1225 }
1226 AggregateKind::RawPtr(..) => {
1227 assert_eq!(field_ops.len(), 2);
1228 let [mut pointer, metadata] = fields.try_into().unwrap();
1229
1230 let mut was_updated = false;
1232 while let Value::Cast { kind: CastKind::PtrToPtr, value: cast_value } =
1233 self.get(pointer)
1234 && let ty::RawPtr(from_pointee_ty, from_mtbl) = self.ty(cast_value).kind()
1235 && let ty::RawPtr(_, output_mtbl) = ty.kind()
1236 && from_mtbl == output_mtbl
1237 && from_pointee_ty.is_sized(self.tcx, self.typing_env())
1238 {
1239 pointer = cast_value;
1240 was_updated = true;
1241 }
1242
1243 if was_updated && let Some(op) = self.try_as_operand(pointer, location) {
1244 field_ops[FieldIdx::ZERO] = op;
1245 }
1246
1247 return Some(self.insert(ty, Value::RawPtr { pointer, metadata }));
1248 }
1249 };
1250
1251 if ty.is_array()
1252 && fields.len() > 4
1253 && let Ok(&first) = fields.iter().all_equal_value()
1254 {
1255 let len = ty::Const::from_target_usize(self.tcx, fields.len().try_into().unwrap());
1256 if let Some(op) = self.try_as_operand(first, location) {
1257 *rvalue = Rvalue::Repeat(op, len);
1258 }
1259 return Some(self.insert(ty, Value::Repeat(first, len)));
1260 }
1261
1262 if let Some(value) = self.simplify_aggregate_to_copy(ty, variant_index, &fields) {
1263 if let Some(place) = self.try_as_place(value, location, true) {
1264 self.reused_locals.insert(place.local);
1265 *rvalue = Rvalue::Use(Operand::Copy(place));
1266 }
1267 return Some(value);
1268 }
1269
1270 Some(self.insert(ty, Value::Aggregate(variant_index, fields)))
1271 }
1272
1273 #[instrument(level = "trace", skip(self), ret)]
1274 fn simplify_unary(
1275 &mut self,
1276 op: UnOp,
1277 arg_op: &mut Operand<'tcx>,
1278 location: Location,
1279 ) -> Option<VnIndex> {
1280 let mut arg_index = self.simplify_operand(arg_op, location)?;
1281 let arg_ty = self.ty(arg_index);
1282 let ret_ty = op.ty(self.tcx, arg_ty);
1283
1284 if op == UnOp::PtrMetadata {
1287 let mut was_updated = false;
1288 loop {
1289 arg_index = match self.get(arg_index) {
1290 Value::Cast { kind: CastKind::PtrToPtr, value: inner }
1299 if self.pointers_have_same_metadata(self.ty(inner), arg_ty) =>
1300 {
1301 inner
1302 }
1303
1304 Value::Cast {
1306 kind: CastKind::PointerCoercion(ty::adjustment::PointerCoercion::Unsize, _),
1307 value: from,
1308 } if let Some(from) = self.ty(from).builtin_deref(true)
1309 && let ty::Array(_, len) = from.kind()
1310 && let Some(to) = self.ty(arg_index).builtin_deref(true)
1311 && let ty::Slice(..) = to.kind() =>
1312 {
1313 return Some(self.insert_constant(Const::Ty(self.tcx.types.usize, *len)));
1314 }
1315
1316 Value::Address { base: AddressBase::Deref(reborrowed), projection, .. }
1318 if projection.is_empty() =>
1319 {
1320 reborrowed
1321 }
1322
1323 _ => break,
1324 };
1325 was_updated = true;
1326 }
1327
1328 if was_updated && let Some(op) = self.try_as_operand(arg_index, location) {
1329 *arg_op = op;
1330 }
1331 }
1332
1333 let value = match (op, self.get(arg_index)) {
1334 (UnOp::Not, Value::UnaryOp(UnOp::Not, inner)) => return Some(inner),
1335 (UnOp::Neg, Value::UnaryOp(UnOp::Neg, inner)) => return Some(inner),
1336 (UnOp::Not, Value::BinaryOp(BinOp::Eq, lhs, rhs)) => {
1337 Value::BinaryOp(BinOp::Ne, lhs, rhs)
1338 }
1339 (UnOp::Not, Value::BinaryOp(BinOp::Ne, lhs, rhs)) => {
1340 Value::BinaryOp(BinOp::Eq, lhs, rhs)
1341 }
1342 (UnOp::PtrMetadata, Value::RawPtr { metadata, .. }) => return Some(metadata),
1343 (
1345 UnOp::PtrMetadata,
1346 Value::Cast {
1347 kind: CastKind::PointerCoercion(ty::adjustment::PointerCoercion::Unsize, _),
1348 value: inner,
1349 },
1350 ) if let ty::Slice(..) = arg_ty.builtin_deref(true).unwrap().kind()
1351 && let ty::Array(_, len) = self.ty(inner).builtin_deref(true).unwrap().kind() =>
1352 {
1353 return Some(self.insert_constant(Const::Ty(self.tcx.types.usize, *len)));
1354 }
1355 _ => Value::UnaryOp(op, arg_index),
1356 };
1357 Some(self.insert(ret_ty, value))
1358 }
1359
1360 #[instrument(level = "trace", skip(self), ret)]
1361 fn simplify_binary(
1362 &mut self,
1363 op: BinOp,
1364 lhs_operand: &mut Operand<'tcx>,
1365 rhs_operand: &mut Operand<'tcx>,
1366 location: Location,
1367 ) -> Option<VnIndex> {
1368 let lhs = self.simplify_operand(lhs_operand, location);
1369 let rhs = self.simplify_operand(rhs_operand, location);
1370
1371 let mut lhs = lhs?;
1374 let mut rhs = rhs?;
1375
1376 let lhs_ty = self.ty(lhs);
1377
1378 if let BinOp::Eq | BinOp::Ne | BinOp::Lt | BinOp::Le | BinOp::Gt | BinOp::Ge = op
1381 && lhs_ty.is_any_ptr()
1382 && let Value::Cast { kind: CastKind::PtrToPtr, value: lhs_value } = self.get(lhs)
1383 && let Value::Cast { kind: CastKind::PtrToPtr, value: rhs_value } = self.get(rhs)
1384 && let lhs_from = self.ty(lhs_value)
1385 && lhs_from == self.ty(rhs_value)
1386 && self.pointers_have_same_metadata(lhs_from, lhs_ty)
1387 {
1388 lhs = lhs_value;
1389 rhs = rhs_value;
1390 if let Some(lhs_op) = self.try_as_operand(lhs, location)
1391 && let Some(rhs_op) = self.try_as_operand(rhs, location)
1392 {
1393 *lhs_operand = lhs_op;
1394 *rhs_operand = rhs_op;
1395 }
1396 }
1397
1398 if let Some(value) = self.simplify_binary_inner(op, lhs_ty, lhs, rhs) {
1399 return Some(value);
1400 }
1401 let ty = op.ty(self.tcx, lhs_ty, self.ty(rhs));
1402 let value = Value::BinaryOp(op, lhs, rhs);
1403 Some(self.insert(ty, value))
1404 }
1405
1406 fn simplify_binary_inner(
1407 &mut self,
1408 op: BinOp,
1409 lhs_ty: Ty<'tcx>,
1410 lhs: VnIndex,
1411 rhs: VnIndex,
1412 ) -> Option<VnIndex> {
1413 let reasonable_ty =
1415 lhs_ty.is_integral() || lhs_ty.is_bool() || lhs_ty.is_char() || lhs_ty.is_any_ptr();
1416 if !reasonable_ty {
1417 return None;
1418 }
1419
1420 let layout = self.ecx.layout_of(lhs_ty).ok()?;
1421
1422 let mut as_bits = |value: VnIndex| {
1423 let constant = self.eval_to_const(value)?;
1424 if layout.backend_repr.is_scalar() {
1425 let scalar = self.ecx.read_scalar(constant).discard_err()?;
1426 scalar.to_bits(constant.layout.size).discard_err()
1427 } else {
1428 None
1430 }
1431 };
1432
1433 use Either::{Left, Right};
1435 let a = as_bits(lhs).map_or(Right(lhs), Left);
1436 let b = as_bits(rhs).map_or(Right(rhs), Left);
1437
1438 let result = match (op, a, b) {
1439 (
1441 BinOp::Add
1442 | BinOp::AddWithOverflow
1443 | BinOp::AddUnchecked
1444 | BinOp::BitOr
1445 | BinOp::BitXor,
1446 Left(0),
1447 Right(p),
1448 )
1449 | (
1450 BinOp::Add
1451 | BinOp::AddWithOverflow
1452 | BinOp::AddUnchecked
1453 | BinOp::BitOr
1454 | BinOp::BitXor
1455 | BinOp::Sub
1456 | BinOp::SubWithOverflow
1457 | BinOp::SubUnchecked
1458 | BinOp::Offset
1459 | BinOp::Shl
1460 | BinOp::Shr,
1461 Right(p),
1462 Left(0),
1463 )
1464 | (BinOp::Mul | BinOp::MulWithOverflow | BinOp::MulUnchecked, Left(1), Right(p))
1465 | (
1466 BinOp::Mul | BinOp::MulWithOverflow | BinOp::MulUnchecked | BinOp::Div,
1467 Right(p),
1468 Left(1),
1469 ) => p,
1470 (BinOp::BitAnd, Right(p), Left(ones)) | (BinOp::BitAnd, Left(ones), Right(p))
1472 if ones == layout.size.truncate(u128::MAX)
1473 || (layout.ty.is_bool() && ones == 1) =>
1474 {
1475 p
1476 }
1477 (
1479 BinOp::Mul | BinOp::MulWithOverflow | BinOp::MulUnchecked | BinOp::BitAnd,
1480 _,
1481 Left(0),
1482 )
1483 | (BinOp::Rem, _, Left(1))
1484 | (
1485 BinOp::Mul
1486 | BinOp::MulWithOverflow
1487 | BinOp::MulUnchecked
1488 | BinOp::Div
1489 | BinOp::Rem
1490 | BinOp::BitAnd
1491 | BinOp::Shl
1492 | BinOp::Shr,
1493 Left(0),
1494 _,
1495 ) => self.insert_scalar(lhs_ty, Scalar::from_uint(0u128, layout.size)),
1496 (BinOp::BitOr, _, Left(ones)) | (BinOp::BitOr, Left(ones), _)
1498 if ones == layout.size.truncate(u128::MAX)
1499 || (layout.ty.is_bool() && ones == 1) =>
1500 {
1501 self.insert_scalar(lhs_ty, Scalar::from_uint(ones, layout.size))
1502 }
1503 (BinOp::Sub | BinOp::SubWithOverflow | BinOp::SubUnchecked | BinOp::BitXor, a, b)
1505 if a == b =>
1506 {
1507 self.insert_scalar(lhs_ty, Scalar::from_uint(0u128, layout.size))
1508 }
1509 (BinOp::Eq, Left(a), Left(b)) => self.insert_bool(a == b),
1514 (BinOp::Eq, a, b) if a == b => self.insert_bool(true),
1515 (BinOp::Ne, Left(a), Left(b)) => self.insert_bool(a != b),
1516 (BinOp::Ne, a, b) if a == b => self.insert_bool(false),
1517 _ => return None,
1518 };
1519
1520 if op.is_overflowing() {
1521 let ty = Ty::new_tup(self.tcx, &[self.ty(result), self.tcx.types.bool]);
1522 let false_val = self.insert_bool(false);
1523 Some(self.insert_tuple(ty, &[result, false_val]))
1524 } else {
1525 Some(result)
1526 }
1527 }
1528
1529 fn simplify_cast(
1530 &mut self,
1531 initial_kind: &mut CastKind,
1532 initial_operand: &mut Operand<'tcx>,
1533 to: Ty<'tcx>,
1534 location: Location,
1535 ) -> Option<VnIndex> {
1536 use CastKind::*;
1537 use rustc_middle::ty::adjustment::PointerCoercion::*;
1538
1539 let mut kind = *initial_kind;
1540 let mut value = self.simplify_operand(initial_operand, location)?;
1541 let mut from = self.ty(value);
1542 if from == to {
1543 return Some(value);
1544 }
1545
1546 if let CastKind::PointerCoercion(ReifyFnPointer(_) | ClosureFnPointer(_), _) = kind {
1547 return Some(self.new_opaque(to));
1550 }
1551
1552 let mut was_ever_updated = false;
1553 loop {
1554 let mut was_updated_this_iteration = false;
1555
1556 if let Transmute = kind
1561 && from.is_raw_ptr()
1562 && to.is_raw_ptr()
1563 && self.pointers_have_same_metadata(from, to)
1564 {
1565 kind = PtrToPtr;
1566 was_updated_this_iteration = true;
1567 }
1568
1569 if let PtrToPtr = kind
1572 && let Value::RawPtr { pointer, .. } = self.get(value)
1573 && let ty::RawPtr(to_pointee, _) = to.kind()
1574 && to_pointee.is_sized(self.tcx, self.typing_env())
1575 {
1576 from = self.ty(pointer);
1577 value = pointer;
1578 was_updated_this_iteration = true;
1579 if from == to {
1580 return Some(pointer);
1581 }
1582 }
1583
1584 if let Transmute = kind
1587 && let Value::Aggregate(variant_idx, field_values) = self.get(value)
1588 && let Some((field_idx, field_ty)) =
1589 self.value_is_all_in_one_field(from, variant_idx)
1590 {
1591 from = field_ty;
1592 value = field_values[field_idx.as_usize()];
1593 was_updated_this_iteration = true;
1594 if field_ty == to {
1595 return Some(value);
1596 }
1597 }
1598
1599 if let Value::Cast { kind: inner_kind, value: inner_value } = self.get(value) {
1601 let inner_from = self.ty(inner_value);
1602 let new_kind = match (inner_kind, kind) {
1603 (PtrToPtr, PtrToPtr) => Some(PtrToPtr),
1607 (PtrToPtr, Transmute) if self.pointers_have_same_metadata(inner_from, from) => {
1611 Some(Transmute)
1612 }
1613 (Transmute, PtrToPtr) if self.pointers_have_same_metadata(from, to) => {
1616 Some(Transmute)
1617 }
1618 (Transmute, Transmute)
1621 if !self.transmute_may_have_niche_of_interest_to_backend(
1622 inner_from, from, to,
1623 ) =>
1624 {
1625 Some(Transmute)
1626 }
1627 _ => None,
1628 };
1629 if let Some(new_kind) = new_kind {
1630 kind = new_kind;
1631 from = inner_from;
1632 value = inner_value;
1633 was_updated_this_iteration = true;
1634 if inner_from == to {
1635 return Some(inner_value);
1636 }
1637 }
1638 }
1639
1640 if was_updated_this_iteration {
1641 was_ever_updated = true;
1642 } else {
1643 break;
1644 }
1645 }
1646
1647 if was_ever_updated && let Some(op) = self.try_as_operand(value, location) {
1648 *initial_operand = op;
1649 *initial_kind = kind;
1650 }
1651
1652 Some(self.insert(to, Value::Cast { kind, value }))
1653 }
1654
1655 fn pointers_have_same_metadata(&self, left_ptr_ty: Ty<'tcx>, right_ptr_ty: Ty<'tcx>) -> bool {
1656 let left_meta_ty = left_ptr_ty.pointee_metadata_ty_or_projection(self.tcx);
1657 let right_meta_ty = right_ptr_ty.pointee_metadata_ty_or_projection(self.tcx);
1658 if left_meta_ty == right_meta_ty {
1659 true
1660 } else if let Ok(left) =
1661 self.tcx.try_normalize_erasing_regions(self.typing_env(), left_meta_ty)
1662 && let Ok(right) =
1663 self.tcx.try_normalize_erasing_regions(self.typing_env(), right_meta_ty)
1664 {
1665 left == right
1666 } else {
1667 false
1668 }
1669 }
1670
1671 fn transmute_may_have_niche_of_interest_to_backend(
1678 &self,
1679 from_ty: Ty<'tcx>,
1680 middle_ty: Ty<'tcx>,
1681 to_ty: Ty<'tcx>,
1682 ) -> bool {
1683 let Ok(middle_layout) = self.ecx.layout_of(middle_ty) else {
1684 return true;
1686 };
1687
1688 if middle_layout.uninhabited {
1689 return true;
1690 }
1691
1692 match middle_layout.backend_repr {
1693 BackendRepr::Scalar(mid) => {
1694 if mid.is_always_valid(&self.ecx) {
1695 false
1698 } else if let Ok(from_layout) = self.ecx.layout_of(from_ty)
1699 && !from_layout.uninhabited
1700 && from_layout.size == middle_layout.size
1701 && let BackendRepr::Scalar(from_a) = from_layout.backend_repr
1702 && let mid_range = mid.valid_range(&self.ecx)
1703 && let from_range = from_a.valid_range(&self.ecx)
1704 && mid_range.contains_range(from_range, middle_layout.size)
1705 {
1706 false
1712 } else if let Ok(to_layout) = self.ecx.layout_of(to_ty)
1713 && !to_layout.uninhabited
1714 && to_layout.size == middle_layout.size
1715 && let BackendRepr::Scalar(to_a) = to_layout.backend_repr
1716 && let mid_range = mid.valid_range(&self.ecx)
1717 && let to_range = to_a.valid_range(&self.ecx)
1718 && mid_range.contains_range(to_range, middle_layout.size)
1719 {
1720 false
1726 } else {
1727 true
1728 }
1729 }
1730 BackendRepr::ScalarPair(a, b) => {
1731 !a.is_always_valid(&self.ecx) || !b.is_always_valid(&self.ecx)
1732 }
1733 BackendRepr::SimdVector { .. }
1734 | BackendRepr::SimdScalableVector { .. }
1735 | BackendRepr::Memory { .. } => false,
1736 }
1737 }
1738
1739 fn value_is_all_in_one_field(
1740 &self,
1741 ty: Ty<'tcx>,
1742 variant: VariantIdx,
1743 ) -> Option<(FieldIdx, Ty<'tcx>)> {
1744 if let Ok(layout) = self.ecx.layout_of(ty)
1745 && let abi::Variants::Single { index } = layout.variants
1746 && index == variant
1747 && let Some((field_idx, field_layout)) = layout.non_1zst_field(&self.ecx)
1748 && layout.size == field_layout.size
1749 {
1750 Some((field_idx, field_layout.ty))
1754 } else if let ty::Adt(adt, args) = ty.kind()
1755 && adt.is_struct()
1756 && adt.repr().transparent()
1757 && let [single_field] = adt.non_enum_variant().fields.raw.as_slice()
1758 {
1759 Some((FieldIdx::ZERO, single_field.ty(self.tcx, args)))
1760 } else {
1761 None
1762 }
1763 }
1764}
1765
1766fn is_deterministic(c: Const<'_>) -> bool {
1775 if c.ty().is_primitive() {
1777 return true;
1778 }
1779
1780 match c {
1781 Const::Ty(..) => false,
1785 Const::Unevaluated(..) => false,
1787 Const::Val(..) => true,
1791 }
1792}
1793
1794fn may_have_provenance(tcx: TyCtxt<'_>, value: ConstValue, size: Size) -> bool {
1797 match value {
1798 ConstValue::ZeroSized | ConstValue::Scalar(Scalar::Int(_)) => return false,
1799 ConstValue::Scalar(Scalar::Ptr(..)) | ConstValue::Slice { .. } => return true,
1800 ConstValue::Indirect { alloc_id, offset } => !tcx
1801 .global_alloc(alloc_id)
1802 .unwrap_memory()
1803 .inner()
1804 .provenance()
1805 .range_empty(AllocRange::from(offset..offset + size), &tcx),
1806 }
1807}
1808
1809fn op_to_prop_const<'tcx>(
1810 ecx: &mut InterpCx<'tcx, DummyMachine>,
1811 op: &OpTy<'tcx>,
1812) -> Option<ConstValue> {
1813 if op.layout.is_unsized() {
1815 return None;
1816 }
1817
1818 if op.layout.is_zst() {
1820 return Some(ConstValue::ZeroSized);
1821 }
1822
1823 if !op.is_immediate_uninit()
1828 && !matches!(op.layout.backend_repr, BackendRepr::Scalar(..) | BackendRepr::ScalarPair(..))
1829 {
1830 return None;
1831 }
1832
1833 if let BackendRepr::Scalar(abi::Scalar::Initialized { .. }) = op.layout.backend_repr
1835 && let Some(scalar) = ecx.read_scalar(op).discard_err()
1836 {
1837 if !scalar.try_to_scalar_int().is_ok() {
1838 return None;
1842 }
1843 return Some(ConstValue::Scalar(scalar));
1844 }
1845
1846 if let Either::Left(mplace) = op.as_mplace_or_imm() {
1849 let (size, _align) = ecx.size_and_align_of_val(&mplace).discard_err()??;
1850
1851 let alloc_ref = ecx.get_ptr_alloc(mplace.ptr(), size).discard_err()??;
1855 if alloc_ref.has_provenance() {
1856 return None;
1857 }
1858
1859 let pointer = mplace.ptr().into_pointer_or_addr().ok()?;
1860 let (prov, offset) = pointer.prov_and_relative_offset();
1861 let alloc_id = prov.alloc_id();
1862 intern_const_alloc_for_constprop(ecx, alloc_id).discard_err()?;
1863
1864 if let GlobalAlloc::Memory(alloc) = ecx.tcx.global_alloc(alloc_id)
1868 && alloc.inner().align >= op.layout.align.abi
1871 {
1872 return Some(ConstValue::Indirect { alloc_id, offset });
1873 }
1874 }
1875
1876 let alloc_id =
1878 ecx.intern_with_temp_alloc(op.layout, |ecx, dest| ecx.copy_op(op, dest)).discard_err()?;
1879 Some(ConstValue::Indirect { alloc_id, offset: Size::ZERO })
1880}
1881
1882impl<'tcx> VnState<'_, '_, 'tcx> {
1883 fn try_as_operand(&mut self, index: VnIndex, location: Location) -> Option<Operand<'tcx>> {
1886 if let Some(const_) = self.try_as_constant(index) {
1887 Some(Operand::Constant(Box::new(const_)))
1888 } else if let Value::RuntimeChecks(c) = self.get(index) {
1889 Some(Operand::RuntimeChecks(c))
1890 } else if let Some(place) = self.try_as_place(index, location, false) {
1891 self.reused_locals.insert(place.local);
1892 Some(Operand::Copy(place))
1893 } else {
1894 None
1895 }
1896 }
1897
1898 fn try_as_constant(&mut self, index: VnIndex) -> Option<ConstOperand<'tcx>> {
1900 let value = self.get(index);
1901
1902 if let Value::Constant { value, disambiguator: None } = value
1904 && let Const::Val(..) = value
1905 {
1906 return Some(ConstOperand { span: DUMMY_SP, user_ty: None, const_: value });
1907 }
1908
1909 if let Some(value) = self.try_as_evaluated_constant(index) {
1910 return Some(ConstOperand { span: DUMMY_SP, user_ty: None, const_: value });
1911 }
1912
1913 if let Value::Constant { value, disambiguator: None } = value {
1915 return Some(ConstOperand { span: DUMMY_SP, user_ty: None, const_: value });
1916 }
1917
1918 None
1919 }
1920
1921 fn try_as_evaluated_constant(&mut self, index: VnIndex) -> Option<Const<'tcx>> {
1922 let op = self.eval_to_const(index)?;
1923 if op.layout.is_unsized() {
1924 return None;
1926 }
1927
1928 let value = op_to_prop_const(&mut self.ecx, op)?;
1929
1930 if may_have_provenance(self.tcx, value, op.layout.size) {
1934 return None;
1935 }
1936
1937 Some(Const::Val(value, op.layout.ty))
1938 }
1939
1940 #[instrument(level = "trace", skip(self), ret)]
1944 fn try_as_place(
1945 &mut self,
1946 mut index: VnIndex,
1947 loc: Location,
1948 allow_complex_projection: bool,
1949 ) -> Option<Place<'tcx>> {
1950 let mut projection = SmallVec::<[PlaceElem<'tcx>; 1]>::new();
1951 loop {
1952 if let Some(local) = self.try_as_local(index, loc) {
1953 projection.reverse();
1954 let place =
1955 Place { local, projection: self.tcx.mk_place_elems(projection.as_slice()) };
1956 return Some(place);
1957 } else if projection.last() == Some(&PlaceElem::Deref) {
1958 return None;
1962 } else if let Value::Projection(pointer, proj) = self.get(index)
1963 && (allow_complex_projection || proj.is_stable_offset())
1964 && let Some(proj) = self.try_as_place_elem(self.ty(index), proj, loc)
1965 {
1966 if proj == PlaceElem::Deref {
1967 match self.get(pointer) {
1970 Value::Argument(_)
1971 if let Some(Mutability::Not) = self.ty(pointer).ref_mutability() => {}
1972 _ => {
1973 return None;
1974 }
1975 }
1976 }
1977 projection.push(proj);
1978 index = pointer;
1979 } else {
1980 return None;
1981 }
1982 }
1983 }
1984
1985 fn try_as_local(&mut self, index: VnIndex, loc: Location) -> Option<Local> {
1988 let other = self.rev_locals.get(index)?;
1989 other
1990 .iter()
1991 .find(|&&other| self.ssa.assignment_dominates(&self.dominators, other, loc))
1992 .copied()
1993 }
1994}
1995
1996impl<'tcx> MutVisitor<'tcx> for VnState<'_, '_, 'tcx> {
1997 fn tcx(&self) -> TyCtxt<'tcx> {
1998 self.tcx
1999 }
2000
2001 fn visit_place(&mut self, place: &mut Place<'tcx>, context: PlaceContext, location: Location) {
2002 self.simplify_place_projection(place, location);
2003 self.super_place(place, context, location);
2004 }
2005
2006 fn visit_operand(&mut self, operand: &mut Operand<'tcx>, location: Location) {
2007 self.simplify_operand(operand, location);
2008 self.super_operand(operand, location);
2009 }
2010
2011 fn visit_assign(
2012 &mut self,
2013 lhs: &mut Place<'tcx>,
2014 rvalue: &mut Rvalue<'tcx>,
2015 location: Location,
2016 ) {
2017 self.simplify_place_projection(lhs, location);
2018
2019 let value = self.simplify_rvalue(lhs, rvalue, location);
2020 if let Some(value) = value {
2021 if let Some(const_) = self.try_as_constant(value) {
2022 *rvalue = Rvalue::Use(Operand::Constant(Box::new(const_)));
2023 } else if let Some(place) = self.try_as_place(value, location, false)
2024 && *rvalue != Rvalue::Use(Operand::Move(place))
2025 && *rvalue != Rvalue::Use(Operand::Copy(place))
2026 {
2027 *rvalue = Rvalue::Use(Operand::Copy(place));
2028 self.reused_locals.insert(place.local);
2029 }
2030 }
2031
2032 if let Some(local) = lhs.as_local()
2033 && self.ssa.is_ssa(local)
2034 && let rvalue_ty = rvalue.ty(self.local_decls, self.tcx)
2035 && self.local_decls[local].ty == rvalue_ty
2038 {
2039 let value = value.unwrap_or_else(|| self.new_opaque(rvalue_ty));
2040 self.assign(local, value);
2041 }
2042 }
2043
2044 fn visit_terminator(&mut self, terminator: &mut Terminator<'tcx>, location: Location) {
2045 if let Terminator { kind: TerminatorKind::Call { destination, .. }, .. } = terminator {
2046 if let Some(local) = destination.as_local()
2047 && self.ssa.is_ssa(local)
2048 {
2049 let ty = self.local_decls[local].ty;
2050 let opaque = self.new_opaque(ty);
2051 self.assign(local, opaque);
2052 }
2053 }
2054 self.super_terminator(terminator, location);
2055 }
2056}
2057
2058struct StorageRemover<'tcx> {
2059 tcx: TyCtxt<'tcx>,
2060 reused_locals: DenseBitSet<Local>,
2061 storage_to_remove: DenseBitSet<Local>,
2062}
2063
2064impl<'tcx> MutVisitor<'tcx> for StorageRemover<'tcx> {
2065 fn tcx(&self) -> TyCtxt<'tcx> {
2066 self.tcx
2067 }
2068
2069 fn visit_operand(&mut self, operand: &mut Operand<'tcx>, _: Location) {
2070 if let Operand::Move(place) = *operand
2071 && !place.is_indirect_first_projection()
2072 && self.reused_locals.contains(place.local)
2073 {
2074 *operand = Operand::Copy(place);
2075 }
2076 }
2077
2078 fn visit_statement(&mut self, stmt: &mut Statement<'tcx>, loc: Location) {
2079 match stmt.kind {
2080 StatementKind::StorageLive(l) | StatementKind::StorageDead(l)
2082 if self.storage_to_remove.contains(l) =>
2083 {
2084 stmt.make_nop(true)
2085 }
2086 _ => self.super_statement(stmt, loc),
2087 }
2088 }
2089}
2090
2091struct StorageChecker<'a, 'tcx> {
2092 reused_locals: &'a DenseBitSet<Local>,
2093 storage_to_remove: DenseBitSet<Local>,
2094 maybe_uninit: ResultsCursor<'a, 'tcx, MaybeUninitializedLocals>,
2095}
2096
2097impl<'a, 'tcx> Visitor<'tcx> for StorageChecker<'a, 'tcx> {
2098 fn visit_local(&mut self, local: Local, context: PlaceContext, location: Location) {
2099 match context {
2100 PlaceContext::MutatingUse(MutatingUseContext::AsmOutput)
2105 | PlaceContext::MutatingUse(MutatingUseContext::Call)
2106 | PlaceContext::MutatingUse(MutatingUseContext::Store)
2107 | PlaceContext::MutatingUse(MutatingUseContext::Yield)
2108 | PlaceContext::NonUse(_) => {
2109 return;
2110 }
2111 PlaceContext::MutatingUse(_) | PlaceContext::NonMutatingUse(_) => {}
2113 }
2114
2115 if !self.reused_locals.contains(local) || self.storage_to_remove.contains(local) {
2117 return;
2118 }
2119
2120 self.maybe_uninit.seek_before_primary_effect(location);
2121
2122 if self.maybe_uninit.get().contains(local) {
2123 debug!(
2124 ?location,
2125 ?local,
2126 "local is reused and is maybe uninit at this location, marking it for storage statement removal"
2127 );
2128 self.storage_to_remove.insert(local);
2129 }
2130 }
2131}