1use std::borrow::Cow;
86
87use either::Either;
88use rustc_abi::{self as abi, BackendRepr, FIRST_VARIANT, FieldIdx, Primitive, Size, VariantIdx};
89use rustc_const_eval::const_eval::DummyMachine;
90use rustc_const_eval::interpret::{
91 ImmTy, Immediate, InterpCx, MemPlaceMeta, MemoryKind, OpTy, Projectable, Scalar,
92 intern_const_alloc_for_constprop,
93};
94use rustc_data_structures::fx::FxIndexSet;
95use rustc_data_structures::graph::dominators::Dominators;
96use rustc_hir::def::DefKind;
97use rustc_index::bit_set::DenseBitSet;
98use rustc_index::{IndexVec, newtype_index};
99use rustc_middle::bug;
100use rustc_middle::mir::interpret::GlobalAlloc;
101use rustc_middle::mir::visit::*;
102use rustc_middle::mir::*;
103use rustc_middle::ty::layout::{HasTypingEnv, LayoutOf};
104use rustc_middle::ty::{self, Ty, TyCtxt};
105use rustc_span::DUMMY_SP;
106use rustc_span::def_id::DefId;
107use smallvec::SmallVec;
108use tracing::{debug, instrument, trace};
109
110use crate::ssa::{AssignedValue, SsaLocals};
111
112pub(super) struct GVN;
113
114impl<'tcx> crate::MirPass<'tcx> for GVN {
115 fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
116 sess.mir_opt_level() >= 2
117 }
118
119 #[instrument(level = "trace", skip(self, tcx, body))]
120 fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
121 debug!(def_id = ?body.source.def_id());
122
123 let typing_env = body.typing_env(tcx);
124 let ssa = SsaLocals::new(tcx, body, typing_env);
125 let dominators = body.basic_blocks.dominators().clone();
127
128 let mut state = VnState::new(tcx, body, typing_env, &ssa, dominators, &body.local_decls);
129 ssa.for_each_assignment_mut(
130 body.basic_blocks.as_mut_preserves_cfg(),
131 |local, value, location| {
132 let value = match value {
133 AssignedValue::Arg | AssignedValue::Terminator => None,
135 AssignedValue::Rvalue(rvalue) => {
137 let value = state.simplify_rvalue(rvalue, location);
138 if state.local_decls[local].ty != rvalue.ty(state.local_decls, tcx) {
141 return;
142 }
143 value
144 }
145 };
146 let value = value.or_else(|| state.new_opaque()).unwrap();
148 state.assign(local, value);
149 },
150 );
151
152 state.next_opaque = None;
154
155 let reverse_postorder = body.basic_blocks.reverse_postorder().to_vec();
156 for bb in reverse_postorder {
157 let data = &mut body.basic_blocks.as_mut_preserves_cfg()[bb];
158 state.visit_basic_block_data(bb, data);
159 }
160
161 StorageRemover { tcx, reused_locals: state.reused_locals }.visit_body_preserves_cfg(body);
165 }
166
167 fn is_required(&self) -> bool {
168 false
169 }
170}
171
172newtype_index! {
173 struct VnIndex {}
174}
175
176#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
179enum AggregateTy<'tcx> {
180 Array,
182 Tuple,
183 Def(DefId, ty::GenericArgsRef<'tcx>),
184 RawPtr {
185 data_pointer_ty: Ty<'tcx>,
187 output_pointer_ty: Ty<'tcx>,
189 },
190}
191
192#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
193enum AddressKind {
194 Ref(BorrowKind),
195 Address(RawPtrKind),
196}
197
198#[derive(Debug, PartialEq, Eq, Hash)]
199enum Value<'tcx> {
200 Opaque(usize),
204 Constant {
206 value: Const<'tcx>,
207 disambiguator: usize,
211 },
212 Aggregate(AggregateTy<'tcx>, VariantIdx, Vec<VnIndex>),
215 Repeat(VnIndex, ty::Const<'tcx>),
217 Address {
219 place: Place<'tcx>,
220 kind: AddressKind,
221 provenance: usize,
223 },
224
225 Projection(VnIndex, ProjectionElem<VnIndex, Ty<'tcx>>),
228 Discriminant(VnIndex),
230 Len(VnIndex),
232
233 NullaryOp(NullOp<'tcx>, Ty<'tcx>),
235 UnaryOp(UnOp, VnIndex),
236 BinaryOp(BinOp, VnIndex, VnIndex),
237 Cast {
238 kind: CastKind,
239 value: VnIndex,
240 from: Ty<'tcx>,
241 to: Ty<'tcx>,
242 },
243}
244
245struct VnState<'body, 'tcx> {
246 tcx: TyCtxt<'tcx>,
247 ecx: InterpCx<'tcx, DummyMachine>,
248 local_decls: &'body LocalDecls<'tcx>,
249 locals: IndexVec<Local, Option<VnIndex>>,
251 rev_locals: IndexVec<VnIndex, SmallVec<[Local; 1]>>,
255 values: FxIndexSet<Value<'tcx>>,
256 evaluated: IndexVec<VnIndex, Option<OpTy<'tcx>>>,
258 next_opaque: Option<usize>,
261 feature_unsized_locals: bool,
263 ssa: &'body SsaLocals,
264 dominators: Dominators<BasicBlock>,
265 reused_locals: DenseBitSet<Local>,
266}
267
268impl<'body, 'tcx> VnState<'body, 'tcx> {
269 fn new(
270 tcx: TyCtxt<'tcx>,
271 body: &Body<'tcx>,
272 typing_env: ty::TypingEnv<'tcx>,
273 ssa: &'body SsaLocals,
274 dominators: Dominators<BasicBlock>,
275 local_decls: &'body LocalDecls<'tcx>,
276 ) -> Self {
277 let num_values =
282 2 * body.basic_blocks.iter().map(|bbdata| bbdata.statements.len()).sum::<usize>()
283 + 4 * body.basic_blocks.len();
284 VnState {
285 tcx,
286 ecx: InterpCx::new(tcx, DUMMY_SP, typing_env, DummyMachine),
287 local_decls,
288 locals: IndexVec::from_elem(None, local_decls),
289 rev_locals: IndexVec::with_capacity(num_values),
290 values: FxIndexSet::with_capacity_and_hasher(num_values, Default::default()),
291 evaluated: IndexVec::with_capacity(num_values),
292 next_opaque: Some(1),
293 feature_unsized_locals: tcx.features().unsized_locals(),
294 ssa,
295 dominators,
296 reused_locals: DenseBitSet::new_empty(local_decls.len()),
297 }
298 }
299
300 fn typing_env(&self) -> ty::TypingEnv<'tcx> {
301 self.ecx.typing_env()
302 }
303
304 #[instrument(level = "trace", skip(self), ret)]
305 fn insert(&mut self, value: Value<'tcx>) -> VnIndex {
306 let (index, new) = self.values.insert_full(value);
307 let index = VnIndex::from_usize(index);
308 if new {
309 let evaluated = self.eval_to_const(index);
311 let _index = self.evaluated.push(evaluated);
312 debug_assert_eq!(index, _index);
313 if self.next_opaque.is_some() {
315 let _index = self.rev_locals.push(SmallVec::new());
316 debug_assert_eq!(index, _index);
317 }
318 }
319 index
320 }
321
322 #[instrument(level = "trace", skip(self), ret)]
325 fn new_opaque(&mut self) -> Option<VnIndex> {
326 let next_opaque = self.next_opaque.as_mut()?;
327 let value = Value::Opaque(*next_opaque);
328 *next_opaque += 1;
329 Some(self.insert(value))
330 }
331
332 #[instrument(level = "trace", skip(self), ret)]
334 fn new_pointer(&mut self, place: Place<'tcx>, kind: AddressKind) -> Option<VnIndex> {
335 let next_opaque = self.next_opaque.as_mut()?;
336 let value = Value::Address { place, kind, provenance: *next_opaque };
337 *next_opaque += 1;
338 Some(self.insert(value))
339 }
340
341 fn get(&self, index: VnIndex) -> &Value<'tcx> {
342 self.values.get_index(index.as_usize()).unwrap()
343 }
344
345 #[instrument(level = "trace", skip(self))]
347 fn assign(&mut self, local: Local, value: VnIndex) {
348 self.locals[local] = Some(value);
349
350 let is_sized = !self.feature_unsized_locals
352 || self.local_decls[local].ty.is_sized(self.tcx, self.typing_env());
353 if is_sized {
354 self.rev_locals[value].push(local);
355 }
356 }
357
358 fn insert_constant(&mut self, value: Const<'tcx>) -> Option<VnIndex> {
359 let disambiguator = if value.is_deterministic() {
360 0
362 } else {
363 let next_opaque = self.next_opaque.as_mut()?;
366 let disambiguator = *next_opaque;
367 *next_opaque += 1;
368 debug_assert_ne!(disambiguator, 0);
370 disambiguator
371 };
372 Some(self.insert(Value::Constant { value, disambiguator }))
373 }
374
375 fn insert_bool(&mut self, flag: bool) -> VnIndex {
376 let value = Const::from_bool(self.tcx, flag);
378 debug_assert!(value.is_deterministic());
379 self.insert(Value::Constant { value, disambiguator: 0 })
380 }
381
382 fn insert_scalar(&mut self, scalar: Scalar, ty: Ty<'tcx>) -> VnIndex {
383 let value = Const::from_scalar(self.tcx, scalar, ty);
385 debug_assert!(value.is_deterministic());
386 self.insert(Value::Constant { value, disambiguator: 0 })
387 }
388
389 fn insert_tuple(&mut self, values: Vec<VnIndex>) -> VnIndex {
390 self.insert(Value::Aggregate(AggregateTy::Tuple, VariantIdx::ZERO, values))
391 }
392
393 #[instrument(level = "trace", skip(self), ret)]
394 fn eval_to_const(&mut self, value: VnIndex) -> Option<OpTy<'tcx>> {
395 use Value::*;
396 let op = match *self.get(value) {
397 Opaque(_) => return None,
398 Repeat(..) => return None,
400
401 Constant { ref value, disambiguator: _ } => {
402 self.ecx.eval_mir_constant(value, DUMMY_SP, None).discard_err()?
403 }
404 Aggregate(kind, variant, ref fields) => {
405 let fields = fields
406 .iter()
407 .map(|&f| self.evaluated[f].as_ref())
408 .collect::<Option<Vec<_>>>()?;
409 let ty = match kind {
410 AggregateTy::Array => {
411 assert!(fields.len() > 0);
412 Ty::new_array(self.tcx, fields[0].layout.ty, fields.len() as u64)
413 }
414 AggregateTy::Tuple => {
415 Ty::new_tup_from_iter(self.tcx, fields.iter().map(|f| f.layout.ty))
416 }
417 AggregateTy::Def(def_id, args) => {
418 self.tcx.type_of(def_id).instantiate(self.tcx, args)
419 }
420 AggregateTy::RawPtr { output_pointer_ty, .. } => output_pointer_ty,
421 };
422 let variant = if ty.is_enum() { Some(variant) } else { None };
423 let ty = self.ecx.layout_of(ty).ok()?;
424 if ty.is_zst() {
425 ImmTy::uninit(ty).into()
426 } else if matches!(kind, AggregateTy::RawPtr { .. }) {
427 let data = self.ecx.read_pointer(fields[0]).discard_err()?;
429 let meta = if fields[1].layout.is_zst() {
430 MemPlaceMeta::None
431 } else {
432 MemPlaceMeta::Meta(self.ecx.read_scalar(fields[1]).discard_err()?)
433 };
434 let ptr_imm = Immediate::new_pointer_with_meta(data, meta, &self.ecx);
435 ImmTy::from_immediate(ptr_imm, ty).into()
436 } else if matches!(
437 ty.backend_repr,
438 BackendRepr::Scalar(..) | BackendRepr::ScalarPair(..)
439 ) {
440 let dest = self.ecx.allocate(ty, MemoryKind::Stack).discard_err()?;
441 let variant_dest = if let Some(variant) = variant {
442 self.ecx.project_downcast(&dest, variant).discard_err()?
443 } else {
444 dest.clone()
445 };
446 for (field_index, op) in fields.into_iter().enumerate() {
447 let field_dest =
448 self.ecx.project_field(&variant_dest, field_index).discard_err()?;
449 self.ecx.copy_op(op, &field_dest).discard_err()?;
450 }
451 self.ecx
452 .write_discriminant(variant.unwrap_or(FIRST_VARIANT), &dest)
453 .discard_err()?;
454 self.ecx
455 .alloc_mark_immutable(dest.ptr().provenance.unwrap().alloc_id())
456 .discard_err()?;
457 dest.into()
458 } else {
459 return None;
460 }
461 }
462
463 Projection(base, elem) => {
464 let value = self.evaluated[base].as_ref()?;
465 let elem = match elem {
466 ProjectionElem::Deref => ProjectionElem::Deref,
467 ProjectionElem::Downcast(name, read_variant) => {
468 ProjectionElem::Downcast(name, read_variant)
469 }
470 ProjectionElem::Field(f, ty) => ProjectionElem::Field(f, ty),
471 ProjectionElem::ConstantIndex { offset, min_length, from_end } => {
472 ProjectionElem::ConstantIndex { offset, min_length, from_end }
473 }
474 ProjectionElem::Subslice { from, to, from_end } => {
475 ProjectionElem::Subslice { from, to, from_end }
476 }
477 ProjectionElem::OpaqueCast(ty) => ProjectionElem::OpaqueCast(ty),
478 ProjectionElem::Subtype(ty) => ProjectionElem::Subtype(ty),
479 ProjectionElem::UnwrapUnsafeBinder(ty) => {
480 ProjectionElem::UnwrapUnsafeBinder(ty)
481 }
482 ProjectionElem::Index(_) => return None,
484 };
485 self.ecx.project(value, elem).discard_err()?
486 }
487 Address { place, kind, provenance: _ } => {
488 if !place.is_indirect_first_projection() {
489 return None;
490 }
491 let local = self.locals[place.local]?;
492 let pointer = self.evaluated[local].as_ref()?;
493 let mut mplace = self.ecx.deref_pointer(pointer).discard_err()?;
494 for proj in place.projection.iter().skip(1) {
495 if matches!(proj, ProjectionElem::Index(_)) {
498 return None;
499 }
500 mplace = self.ecx.project(&mplace, proj).discard_err()?;
501 }
502 let pointer = mplace.to_ref(&self.ecx);
503 let ty = match kind {
504 AddressKind::Ref(bk) => Ty::new_ref(
505 self.tcx,
506 self.tcx.lifetimes.re_erased,
507 mplace.layout.ty,
508 bk.to_mutbl_lossy(),
509 ),
510 AddressKind::Address(mutbl) => {
511 Ty::new_ptr(self.tcx, mplace.layout.ty, mutbl.to_mutbl_lossy())
512 }
513 };
514 let layout = self.ecx.layout_of(ty).ok()?;
515 ImmTy::from_immediate(pointer, layout).into()
516 }
517
518 Discriminant(base) => {
519 let base = self.evaluated[base].as_ref()?;
520 let variant = self.ecx.read_discriminant(base).discard_err()?;
521 let discr_value =
522 self.ecx.discriminant_for_variant(base.layout.ty, variant).discard_err()?;
523 discr_value.into()
524 }
525 Len(slice) => {
526 let slice = self.evaluated[slice].as_ref()?;
527 let usize_layout = self.ecx.layout_of(self.tcx.types.usize).unwrap();
528 let len = slice.len(&self.ecx).discard_err()?;
529 let imm = ImmTy::from_uint(len, usize_layout);
530 imm.into()
531 }
532 NullaryOp(null_op, ty) => {
533 let layout = self.ecx.layout_of(ty).ok()?;
534 if let NullOp::SizeOf | NullOp::AlignOf = null_op
535 && layout.is_unsized()
536 {
537 return None;
538 }
539 let val = match null_op {
540 NullOp::SizeOf => layout.size.bytes(),
541 NullOp::AlignOf => layout.align.abi.bytes(),
542 NullOp::OffsetOf(fields) => self
543 .ecx
544 .tcx
545 .offset_of_subfield(self.typing_env(), layout, fields.iter())
546 .bytes(),
547 NullOp::UbChecks => return None,
548 NullOp::ContractChecks => return None,
549 };
550 let usize_layout = self.ecx.layout_of(self.tcx.types.usize).unwrap();
551 let imm = ImmTy::from_uint(val, usize_layout);
552 imm.into()
553 }
554 UnaryOp(un_op, operand) => {
555 let operand = self.evaluated[operand].as_ref()?;
556 let operand = self.ecx.read_immediate(operand).discard_err()?;
557 let val = self.ecx.unary_op(un_op, &operand).discard_err()?;
558 val.into()
559 }
560 BinaryOp(bin_op, lhs, rhs) => {
561 let lhs = self.evaluated[lhs].as_ref()?;
562 let lhs = self.ecx.read_immediate(lhs).discard_err()?;
563 let rhs = self.evaluated[rhs].as_ref()?;
564 let rhs = self.ecx.read_immediate(rhs).discard_err()?;
565 let val = self.ecx.binary_op(bin_op, &lhs, &rhs).discard_err()?;
566 val.into()
567 }
568 Cast { kind, value, from: _, to } => match kind {
569 CastKind::IntToInt | CastKind::IntToFloat => {
570 let value = self.evaluated[value].as_ref()?;
571 let value = self.ecx.read_immediate(value).discard_err()?;
572 let to = self.ecx.layout_of(to).ok()?;
573 let res = self.ecx.int_to_int_or_float(&value, to).discard_err()?;
574 res.into()
575 }
576 CastKind::FloatToFloat | CastKind::FloatToInt => {
577 let value = self.evaluated[value].as_ref()?;
578 let value = self.ecx.read_immediate(value).discard_err()?;
579 let to = self.ecx.layout_of(to).ok()?;
580 let res = self.ecx.float_to_float_or_int(&value, to).discard_err()?;
581 res.into()
582 }
583 CastKind::Transmute => {
584 let value = self.evaluated[value].as_ref()?;
585 let to = self.ecx.layout_of(to).ok()?;
586 if value.as_mplace_or_imm().is_right() {
591 let can_transmute = match (value.layout.backend_repr, to.backend_repr) {
592 (BackendRepr::Scalar(s1), BackendRepr::Scalar(s2)) => {
593 s1.size(&self.ecx) == s2.size(&self.ecx)
594 && !matches!(s1.primitive(), Primitive::Pointer(..))
595 }
596 (BackendRepr::ScalarPair(a1, b1), BackendRepr::ScalarPair(a2, b2)) => {
597 a1.size(&self.ecx) == a2.size(&self.ecx) &&
598 b1.size(&self.ecx) == b2.size(&self.ecx) &&
599 b1.align(&self.ecx) == b2.align(&self.ecx) &&
601 !matches!(a1.primitive(), Primitive::Pointer(..))
603 && !matches!(b1.primitive(), Primitive::Pointer(..))
604 }
605 _ => false,
606 };
607 if !can_transmute {
608 return None;
609 }
610 }
611 value.offset(Size::ZERO, to, &self.ecx).discard_err()?
612 }
613 CastKind::PointerCoercion(ty::adjustment::PointerCoercion::Unsize, _) => {
614 let src = self.evaluated[value].as_ref()?;
615 let to = self.ecx.layout_of(to).ok()?;
616 let dest = self.ecx.allocate(to, MemoryKind::Stack).discard_err()?;
617 self.ecx.unsize_into(src, to, &dest.clone().into()).discard_err()?;
618 self.ecx
619 .alloc_mark_immutable(dest.ptr().provenance.unwrap().alloc_id())
620 .discard_err()?;
621 dest.into()
622 }
623 CastKind::FnPtrToPtr | CastKind::PtrToPtr => {
624 let src = self.evaluated[value].as_ref()?;
625 let src = self.ecx.read_immediate(src).discard_err()?;
626 let to = self.ecx.layout_of(to).ok()?;
627 let ret = self.ecx.ptr_to_ptr(&src, to).discard_err()?;
628 ret.into()
629 }
630 CastKind::PointerCoercion(ty::adjustment::PointerCoercion::UnsafeFnPointer, _) => {
631 let src = self.evaluated[value].as_ref()?;
632 let src = self.ecx.read_immediate(src).discard_err()?;
633 let to = self.ecx.layout_of(to).ok()?;
634 ImmTy::from_immediate(*src, to).into()
635 }
636 _ => return None,
637 },
638 };
639 Some(op)
640 }
641
642 fn project(
643 &mut self,
644 place: PlaceRef<'tcx>,
645 value: VnIndex,
646 proj: PlaceElem<'tcx>,
647 ) -> Option<VnIndex> {
648 let proj = match proj {
649 ProjectionElem::Deref => {
650 let ty = place.ty(self.local_decls, self.tcx).ty;
651 if self.tcx.sess.opts.unstable_opts.unsound_mir_opts
653 && let Some(Mutability::Not) = ty.ref_mutability()
654 && let Some(pointee_ty) = ty.builtin_deref(true)
655 && pointee_ty.is_freeze(self.tcx, self.typing_env())
656 {
657 ProjectionElem::Deref
660 } else {
661 return None;
662 }
663 }
664 ProjectionElem::Downcast(name, index) => ProjectionElem::Downcast(name, index),
665 ProjectionElem::Field(f, ty) => {
666 if let Value::Aggregate(_, _, fields) = self.get(value) {
667 return Some(fields[f.as_usize()]);
668 } else if let Value::Projection(outer_value, ProjectionElem::Downcast(_, read_variant)) = self.get(value)
669 && let Value::Aggregate(_, written_variant, fields) = self.get(*outer_value)
670 && written_variant == read_variant
686 {
687 return Some(fields[f.as_usize()]);
688 }
689 ProjectionElem::Field(f, ty)
690 }
691 ProjectionElem::Index(idx) => {
692 if let Value::Repeat(inner, _) = self.get(value) {
693 return Some(*inner);
694 }
695 let idx = self.locals[idx]?;
696 ProjectionElem::Index(idx)
697 }
698 ProjectionElem::ConstantIndex { offset, min_length, from_end } => {
699 match self.get(value) {
700 Value::Repeat(inner, _) => {
701 return Some(*inner);
702 }
703 Value::Aggregate(AggregateTy::Array, _, operands) => {
704 let offset = if from_end {
705 operands.len() - offset as usize
706 } else {
707 offset as usize
708 };
709 return operands.get(offset).copied();
710 }
711 _ => {}
712 };
713 ProjectionElem::ConstantIndex { offset, min_length, from_end }
714 }
715 ProjectionElem::Subslice { from, to, from_end } => {
716 ProjectionElem::Subslice { from, to, from_end }
717 }
718 ProjectionElem::OpaqueCast(ty) => ProjectionElem::OpaqueCast(ty),
719 ProjectionElem::Subtype(ty) => ProjectionElem::Subtype(ty),
720 ProjectionElem::UnwrapUnsafeBinder(ty) => ProjectionElem::UnwrapUnsafeBinder(ty),
721 };
722
723 Some(self.insert(Value::Projection(value, proj)))
724 }
725
726 #[instrument(level = "trace", skip(self))]
728 fn simplify_place_projection(&mut self, place: &mut Place<'tcx>, location: Location) {
729 if place.is_indirect_first_projection()
732 && let Some(base) = self.locals[place.local]
733 && let Some(new_local) = self.try_as_local(base, location)
734 && place.local != new_local
735 {
736 place.local = new_local;
737 self.reused_locals.insert(new_local);
738 }
739
740 let mut projection = Cow::Borrowed(&place.projection[..]);
741
742 for i in 0..projection.len() {
743 let elem = projection[i];
744 if let ProjectionElem::Index(idx_local) = elem
745 && let Some(idx) = self.locals[idx_local]
746 {
747 if let Some(offset) = self.evaluated[idx].as_ref()
748 && let Some(offset) = self.ecx.read_target_usize(offset).discard_err()
749 && let Some(min_length) = offset.checked_add(1)
750 {
751 projection.to_mut()[i] =
752 ProjectionElem::ConstantIndex { offset, min_length, from_end: false };
753 } else if let Some(new_idx_local) = self.try_as_local(idx, location)
754 && idx_local != new_idx_local
755 {
756 projection.to_mut()[i] = ProjectionElem::Index(new_idx_local);
757 self.reused_locals.insert(new_idx_local);
758 }
759 }
760 }
761
762 if projection.is_owned() {
763 place.projection = self.tcx.mk_place_elems(&projection);
764 }
765
766 trace!(?place);
767 }
768
769 #[instrument(level = "trace", skip(self), ret)]
772 fn simplify_place_value(
773 &mut self,
774 place: &mut Place<'tcx>,
775 location: Location,
776 ) -> Option<VnIndex> {
777 self.simplify_place_projection(place, location);
778
779 let mut place_ref = place.as_ref();
782
783 let mut value = self.locals[place.local]?;
785 for (index, proj) in place.projection.iter().enumerate() {
786 if let Value::Projection(pointer, ProjectionElem::Deref) = *self.get(value)
787 && let Value::Address { place: mut pointee, kind, .. } = *self.get(pointer)
788 && let AddressKind::Ref(BorrowKind::Shared) = kind
789 && let Some(v) = self.simplify_place_value(&mut pointee, location)
790 {
791 value = v;
792 place_ref = pointee.project_deeper(&place.projection[index..], self.tcx).as_ref();
793 }
794 if let Some(local) = self.try_as_local(value, location) {
795 place_ref = PlaceRef { local, projection: &place.projection[index..] };
799 }
800
801 let base = PlaceRef { local: place.local, projection: &place.projection[..index] };
802 value = self.project(base, value, proj)?;
803 }
804
805 if let Value::Projection(pointer, ProjectionElem::Deref) = *self.get(value)
806 && let Value::Address { place: mut pointee, kind, .. } = *self.get(pointer)
807 && let AddressKind::Ref(BorrowKind::Shared) = kind
808 && let Some(v) = self.simplify_place_value(&mut pointee, location)
809 {
810 value = v;
811 place_ref = pointee.project_deeper(&[], self.tcx).as_ref();
812 }
813 if let Some(new_local) = self.try_as_local(value, location) {
814 place_ref = PlaceRef { local: new_local, projection: &[] };
815 }
816
817 if place_ref.local != place.local || place_ref.projection.len() < place.projection.len() {
818 *place = place_ref.project_deeper(&[], self.tcx);
820 self.reused_locals.insert(place_ref.local);
821 }
822
823 Some(value)
824 }
825
826 #[instrument(level = "trace", skip(self), ret)]
827 fn simplify_operand(
828 &mut self,
829 operand: &mut Operand<'tcx>,
830 location: Location,
831 ) -> Option<VnIndex> {
832 match *operand {
833 Operand::Constant(ref constant) => self.insert_constant(constant.const_),
834 Operand::Copy(ref mut place) | Operand::Move(ref mut place) => {
835 let value = self.simplify_place_value(place, location)?;
836 if let Some(const_) = self.try_as_constant(value) {
837 *operand = Operand::Constant(Box::new(const_));
838 }
839 Some(value)
840 }
841 }
842 }
843
844 #[instrument(level = "trace", skip(self), ret)]
845 fn simplify_rvalue(
846 &mut self,
847 rvalue: &mut Rvalue<'tcx>,
848 location: Location,
849 ) -> Option<VnIndex> {
850 let value = match *rvalue {
851 Rvalue::Use(ref mut operand) => return self.simplify_operand(operand, location),
853 Rvalue::CopyForDeref(place) => {
854 let mut operand = Operand::Copy(place);
855 let val = self.simplify_operand(&mut operand, location);
856 *rvalue = Rvalue::Use(operand);
857 return val;
858 }
859
860 Rvalue::Repeat(ref mut op, amount) => {
862 let op = self.simplify_operand(op, location)?;
863 Value::Repeat(op, amount)
864 }
865 Rvalue::NullaryOp(op, ty) => Value::NullaryOp(op, ty),
866 Rvalue::Aggregate(..) => return self.simplify_aggregate(rvalue, location),
867 Rvalue::Ref(_, borrow_kind, ref mut place) => {
868 self.simplify_place_projection(place, location);
869 return self.new_pointer(*place, AddressKind::Ref(borrow_kind));
870 }
871 Rvalue::RawPtr(mutbl, ref mut place) => {
872 self.simplify_place_projection(place, location);
873 return self.new_pointer(*place, AddressKind::Address(mutbl));
874 }
875 Rvalue::WrapUnsafeBinder(ref mut op, ty) => {
876 let value = self.simplify_operand(op, location)?;
877 Value::Cast {
878 kind: CastKind::Transmute,
879 value,
880 from: op.ty(self.local_decls, self.tcx),
881 to: ty,
882 }
883 }
884
885 Rvalue::Len(ref mut place) => return self.simplify_len(place, location),
887 Rvalue::Cast(ref mut kind, ref mut value, to) => {
888 return self.simplify_cast(kind, value, to, location);
889 }
890 Rvalue::BinaryOp(op, box (ref mut lhs, ref mut rhs)) => {
891 return self.simplify_binary(op, lhs, rhs, location);
892 }
893 Rvalue::UnaryOp(op, ref mut arg_op) => {
894 return self.simplify_unary(op, arg_op, location);
895 }
896 Rvalue::Discriminant(ref mut place) => {
897 let place = self.simplify_place_value(place, location)?;
898 if let Some(discr) = self.simplify_discriminant(place) {
899 return Some(discr);
900 }
901 Value::Discriminant(place)
902 }
903
904 Rvalue::ThreadLocalRef(..) | Rvalue::ShallowInitBox(..) => return None,
906 };
907 debug!(?value);
908 Some(self.insert(value))
909 }
910
911 fn simplify_discriminant(&mut self, place: VnIndex) -> Option<VnIndex> {
912 if let Value::Aggregate(enum_ty, variant, _) = *self.get(place)
913 && let AggregateTy::Def(enum_did, enum_args) = enum_ty
914 && let DefKind::Enum = self.tcx.def_kind(enum_did)
915 {
916 let enum_ty = self.tcx.type_of(enum_did).instantiate(self.tcx, enum_args);
917 let discr = self.ecx.discriminant_for_variant(enum_ty, variant).discard_err()?;
918 return Some(self.insert_scalar(discr.to_scalar(), discr.layout.ty));
919 }
920
921 None
922 }
923
924 fn try_as_place_elem(
925 &mut self,
926 proj: ProjectionElem<VnIndex, Ty<'tcx>>,
927 loc: Location,
928 ) -> Option<PlaceElem<'tcx>> {
929 Some(match proj {
930 ProjectionElem::Deref => ProjectionElem::Deref,
931 ProjectionElem::Field(idx, ty) => ProjectionElem::Field(idx, ty),
932 ProjectionElem::Index(idx) => {
933 let Some(local) = self.try_as_local(idx, loc) else {
934 return None;
935 };
936 self.reused_locals.insert(local);
937 ProjectionElem::Index(local)
938 }
939 ProjectionElem::ConstantIndex { offset, min_length, from_end } => {
940 ProjectionElem::ConstantIndex { offset, min_length, from_end }
941 }
942 ProjectionElem::Subslice { from, to, from_end } => {
943 ProjectionElem::Subslice { from, to, from_end }
944 }
945 ProjectionElem::Downcast(symbol, idx) => ProjectionElem::Downcast(symbol, idx),
946 ProjectionElem::OpaqueCast(idx) => ProjectionElem::OpaqueCast(idx),
947 ProjectionElem::Subtype(idx) => ProjectionElem::Subtype(idx),
948 ProjectionElem::UnwrapUnsafeBinder(ty) => ProjectionElem::UnwrapUnsafeBinder(ty),
949 })
950 }
951
952 fn simplify_aggregate_to_copy(
953 &mut self,
954 rvalue: &mut Rvalue<'tcx>,
955 location: Location,
956 fields: &[VnIndex],
957 variant_index: VariantIdx,
958 ) -> Option<VnIndex> {
959 let Some(&first_field) = fields.first() else {
960 return None;
961 };
962 let Value::Projection(copy_from_value, _) = *self.get(first_field) else {
963 return None;
964 };
965 if fields.iter().enumerate().any(|(index, &v)| {
967 if let Value::Projection(pointer, ProjectionElem::Field(from_index, _)) = *self.get(v)
968 && copy_from_value == pointer
969 && from_index.index() == index
970 {
971 return false;
972 }
973 true
974 }) {
975 return None;
976 }
977
978 let mut copy_from_local_value = copy_from_value;
979 if let Value::Projection(pointer, proj) = *self.get(copy_from_value)
980 && let ProjectionElem::Downcast(_, read_variant) = proj
981 {
982 if variant_index == read_variant {
983 copy_from_local_value = pointer;
985 } else {
986 return None;
988 }
989 }
990
991 let tcx = self.tcx;
992 let mut projection = SmallVec::<[PlaceElem<'tcx>; 1]>::new();
993 loop {
994 if let Some(local) = self.try_as_local(copy_from_local_value, location) {
995 projection.reverse();
996 let place = Place { local, projection: tcx.mk_place_elems(projection.as_slice()) };
997 if rvalue.ty(self.local_decls, tcx) == place.ty(self.local_decls, tcx).ty {
998 self.reused_locals.insert(local);
999 *rvalue = Rvalue::Use(Operand::Copy(place));
1000 return Some(copy_from_value);
1001 }
1002 return None;
1003 } else if let Value::Projection(pointer, proj) = *self.get(copy_from_local_value)
1004 && let Some(proj) = self.try_as_place_elem(proj, location)
1005 {
1006 projection.push(proj);
1007 copy_from_local_value = pointer;
1008 } else {
1009 return None;
1010 }
1011 }
1012 }
1013
1014 fn simplify_aggregate(
1015 &mut self,
1016 rvalue: &mut Rvalue<'tcx>,
1017 location: Location,
1018 ) -> Option<VnIndex> {
1019 let Rvalue::Aggregate(box ref kind, ref mut field_ops) = *rvalue else { bug!() };
1020
1021 let tcx = self.tcx;
1022 if field_ops.is_empty() {
1023 let is_zst = match *kind {
1024 AggregateKind::Array(..)
1025 | AggregateKind::Tuple
1026 | AggregateKind::Closure(..)
1027 | AggregateKind::CoroutineClosure(..) => true,
1028 AggregateKind::Adt(did, ..) => tcx.def_kind(did) != DefKind::Enum,
1030 AggregateKind::Coroutine(..) => false,
1032 AggregateKind::RawPtr(..) => bug!("MIR for RawPtr aggregate must have 2 fields"),
1033 };
1034
1035 if is_zst {
1036 let ty = rvalue.ty(self.local_decls, tcx);
1037 return self.insert_constant(Const::zero_sized(ty));
1038 }
1039 }
1040
1041 let (mut ty, variant_index) = match *kind {
1042 AggregateKind::Array(..) => {
1043 assert!(!field_ops.is_empty());
1044 (AggregateTy::Array, FIRST_VARIANT)
1045 }
1046 AggregateKind::Tuple => {
1047 assert!(!field_ops.is_empty());
1048 (AggregateTy::Tuple, FIRST_VARIANT)
1049 }
1050 AggregateKind::Closure(did, args)
1051 | AggregateKind::CoroutineClosure(did, args)
1052 | AggregateKind::Coroutine(did, args) => (AggregateTy::Def(did, args), FIRST_VARIANT),
1053 AggregateKind::Adt(did, variant_index, args, _, None) => {
1054 (AggregateTy::Def(did, args), variant_index)
1055 }
1056 AggregateKind::Adt(_, _, _, _, Some(_)) => return None,
1058 AggregateKind::RawPtr(pointee_ty, mtbl) => {
1059 assert_eq!(field_ops.len(), 2);
1060 let data_pointer_ty = field_ops[FieldIdx::ZERO].ty(self.local_decls, self.tcx);
1061 let output_pointer_ty = Ty::new_ptr(self.tcx, pointee_ty, mtbl);
1062 (AggregateTy::RawPtr { data_pointer_ty, output_pointer_ty }, FIRST_VARIANT)
1063 }
1064 };
1065
1066 let fields: Option<Vec<_>> = field_ops
1067 .iter_mut()
1068 .map(|op| self.simplify_operand(op, location).or_else(|| self.new_opaque()))
1069 .collect();
1070 let mut fields = fields?;
1071
1072 if let AggregateTy::RawPtr { data_pointer_ty, output_pointer_ty } = &mut ty {
1073 let mut was_updated = false;
1074
1075 while let Value::Cast {
1077 kind: CastKind::PtrToPtr,
1078 value: cast_value,
1079 from: cast_from,
1080 to: _,
1081 } = self.get(fields[0])
1082 && let ty::RawPtr(from_pointee_ty, from_mtbl) = cast_from.kind()
1083 && let ty::RawPtr(_, output_mtbl) = output_pointer_ty.kind()
1084 && from_mtbl == output_mtbl
1085 && from_pointee_ty.is_sized(self.tcx, self.typing_env())
1086 {
1087 fields[0] = *cast_value;
1088 *data_pointer_ty = *cast_from;
1089 was_updated = true;
1090 }
1091
1092 if was_updated && let Some(op) = self.try_as_operand(fields[0], location) {
1093 field_ops[FieldIdx::ZERO] = op;
1094 }
1095 }
1096
1097 if let AggregateTy::Array = ty
1098 && fields.len() > 4
1099 {
1100 let first = fields[0];
1101 if fields.iter().all(|&v| v == first) {
1102 let len = ty::Const::from_target_usize(self.tcx, fields.len().try_into().unwrap());
1103 if let Some(op) = self.try_as_operand(first, location) {
1104 *rvalue = Rvalue::Repeat(op, len);
1105 }
1106 return Some(self.insert(Value::Repeat(first, len)));
1107 }
1108 }
1109
1110 if tcx.sess.opts.unstable_opts.unsound_mir_opts
1112 && let AggregateTy::Def(_, _) = ty
1113 && let Some(value) =
1114 self.simplify_aggregate_to_copy(rvalue, location, &fields, variant_index)
1115 {
1116 return Some(value);
1117 }
1118
1119 Some(self.insert(Value::Aggregate(ty, variant_index, fields)))
1120 }
1121
1122 #[instrument(level = "trace", skip(self), ret)]
1123 fn simplify_unary(
1124 &mut self,
1125 op: UnOp,
1126 arg_op: &mut Operand<'tcx>,
1127 location: Location,
1128 ) -> Option<VnIndex> {
1129 let mut arg_index = self.simplify_operand(arg_op, location)?;
1130
1131 if op == UnOp::PtrMetadata {
1134 let mut was_updated = false;
1135 loop {
1136 match self.get(arg_index) {
1137 Value::Cast { kind: CastKind::PtrToPtr, value: inner, from, to }
1146 if self.pointers_have_same_metadata(*from, *to) =>
1147 {
1148 arg_index = *inner;
1149 was_updated = true;
1150 continue;
1151 }
1152
1153 Value::Address { place, kind: _, provenance: _ }
1155 if let PlaceRef { local, projection: [PlaceElem::Deref] } =
1156 place.as_ref()
1157 && let Some(local_index) = self.locals[local] =>
1158 {
1159 arg_index = local_index;
1160 was_updated = true;
1161 continue;
1162 }
1163
1164 _ => {
1165 if was_updated && let Some(op) = self.try_as_operand(arg_index, location) {
1166 *arg_op = op;
1167 }
1168 break;
1169 }
1170 }
1171 }
1172 }
1173
1174 let value = match (op, self.get(arg_index)) {
1175 (UnOp::Not, Value::UnaryOp(UnOp::Not, inner)) => return Some(*inner),
1176 (UnOp::Neg, Value::UnaryOp(UnOp::Neg, inner)) => return Some(*inner),
1177 (UnOp::Not, Value::BinaryOp(BinOp::Eq, lhs, rhs)) => {
1178 Value::BinaryOp(BinOp::Ne, *lhs, *rhs)
1179 }
1180 (UnOp::Not, Value::BinaryOp(BinOp::Ne, lhs, rhs)) => {
1181 Value::BinaryOp(BinOp::Eq, *lhs, *rhs)
1182 }
1183 (UnOp::PtrMetadata, Value::Aggregate(AggregateTy::RawPtr { .. }, _, fields)) => {
1184 return Some(fields[1]);
1185 }
1186 (
1188 UnOp::PtrMetadata,
1189 Value::Cast {
1190 kind: CastKind::PointerCoercion(ty::adjustment::PointerCoercion::Unsize, _),
1191 from,
1192 to,
1193 ..
1194 },
1195 ) if let ty::Slice(..) = to.builtin_deref(true).unwrap().kind()
1196 && let ty::Array(_, len) = from.builtin_deref(true).unwrap().kind() =>
1197 {
1198 return self.insert_constant(Const::Ty(self.tcx.types.usize, *len));
1199 }
1200 _ => Value::UnaryOp(op, arg_index),
1201 };
1202 Some(self.insert(value))
1203 }
1204
1205 #[instrument(level = "trace", skip(self), ret)]
1206 fn simplify_binary(
1207 &mut self,
1208 op: BinOp,
1209 lhs_operand: &mut Operand<'tcx>,
1210 rhs_operand: &mut Operand<'tcx>,
1211 location: Location,
1212 ) -> Option<VnIndex> {
1213 let lhs = self.simplify_operand(lhs_operand, location);
1214 let rhs = self.simplify_operand(rhs_operand, location);
1215 let mut lhs = lhs?;
1218 let mut rhs = rhs?;
1219
1220 let lhs_ty = lhs_operand.ty(self.local_decls, self.tcx);
1221
1222 if let BinOp::Eq | BinOp::Ne | BinOp::Lt | BinOp::Le | BinOp::Gt | BinOp::Ge = op
1225 && lhs_ty.is_any_ptr()
1226 && let Value::Cast {
1227 kind: CastKind::PtrToPtr, value: lhs_value, from: lhs_from, ..
1228 } = self.get(lhs)
1229 && let Value::Cast {
1230 kind: CastKind::PtrToPtr, value: rhs_value, from: rhs_from, ..
1231 } = self.get(rhs)
1232 && lhs_from == rhs_from
1233 && self.pointers_have_same_metadata(*lhs_from, lhs_ty)
1234 {
1235 lhs = *lhs_value;
1236 rhs = *rhs_value;
1237 if let Some(lhs_op) = self.try_as_operand(lhs, location)
1238 && let Some(rhs_op) = self.try_as_operand(rhs, location)
1239 {
1240 *lhs_operand = lhs_op;
1241 *rhs_operand = rhs_op;
1242 }
1243 }
1244
1245 if let Some(value) = self.simplify_binary_inner(op, lhs_ty, lhs, rhs) {
1246 return Some(value);
1247 }
1248 let value = Value::BinaryOp(op, lhs, rhs);
1249 Some(self.insert(value))
1250 }
1251
1252 fn simplify_binary_inner(
1253 &mut self,
1254 op: BinOp,
1255 lhs_ty: Ty<'tcx>,
1256 lhs: VnIndex,
1257 rhs: VnIndex,
1258 ) -> Option<VnIndex> {
1259 let reasonable_ty =
1261 lhs_ty.is_integral() || lhs_ty.is_bool() || lhs_ty.is_char() || lhs_ty.is_any_ptr();
1262 if !reasonable_ty {
1263 return None;
1264 }
1265
1266 let layout = self.ecx.layout_of(lhs_ty).ok()?;
1267
1268 let as_bits = |value: VnIndex| {
1269 let constant = self.evaluated[value].as_ref()?;
1270 if layout.backend_repr.is_scalar() {
1271 let scalar = self.ecx.read_scalar(constant).discard_err()?;
1272 scalar.to_bits(constant.layout.size).discard_err()
1273 } else {
1274 None
1276 }
1277 };
1278
1279 use Either::{Left, Right};
1281 let a = as_bits(lhs).map_or(Right(lhs), Left);
1282 let b = as_bits(rhs).map_or(Right(rhs), Left);
1283
1284 let result = match (op, a, b) {
1285 (
1287 BinOp::Add
1288 | BinOp::AddWithOverflow
1289 | BinOp::AddUnchecked
1290 | BinOp::BitOr
1291 | BinOp::BitXor,
1292 Left(0),
1293 Right(p),
1294 )
1295 | (
1296 BinOp::Add
1297 | BinOp::AddWithOverflow
1298 | BinOp::AddUnchecked
1299 | BinOp::BitOr
1300 | BinOp::BitXor
1301 | BinOp::Sub
1302 | BinOp::SubWithOverflow
1303 | BinOp::SubUnchecked
1304 | BinOp::Offset
1305 | BinOp::Shl
1306 | BinOp::Shr,
1307 Right(p),
1308 Left(0),
1309 )
1310 | (BinOp::Mul | BinOp::MulWithOverflow | BinOp::MulUnchecked, Left(1), Right(p))
1311 | (
1312 BinOp::Mul | BinOp::MulWithOverflow | BinOp::MulUnchecked | BinOp::Div,
1313 Right(p),
1314 Left(1),
1315 ) => p,
1316 (BinOp::BitAnd, Right(p), Left(ones)) | (BinOp::BitAnd, Left(ones), Right(p))
1318 if ones == layout.size.truncate(u128::MAX)
1319 || (layout.ty.is_bool() && ones == 1) =>
1320 {
1321 p
1322 }
1323 (
1325 BinOp::Mul | BinOp::MulWithOverflow | BinOp::MulUnchecked | BinOp::BitAnd,
1326 _,
1327 Left(0),
1328 )
1329 | (BinOp::Rem, _, Left(1))
1330 | (
1331 BinOp::Mul
1332 | BinOp::MulWithOverflow
1333 | BinOp::MulUnchecked
1334 | BinOp::Div
1335 | BinOp::Rem
1336 | BinOp::BitAnd
1337 | BinOp::Shl
1338 | BinOp::Shr,
1339 Left(0),
1340 _,
1341 ) => self.insert_scalar(Scalar::from_uint(0u128, layout.size), lhs_ty),
1342 (BinOp::BitOr, _, Left(ones)) | (BinOp::BitOr, Left(ones), _)
1344 if ones == layout.size.truncate(u128::MAX)
1345 || (layout.ty.is_bool() && ones == 1) =>
1346 {
1347 self.insert_scalar(Scalar::from_uint(ones, layout.size), lhs_ty)
1348 }
1349 (BinOp::Sub | BinOp::SubWithOverflow | BinOp::SubUnchecked | BinOp::BitXor, a, b)
1351 if a == b =>
1352 {
1353 self.insert_scalar(Scalar::from_uint(0u128, layout.size), lhs_ty)
1354 }
1355 (BinOp::Eq, Left(a), Left(b)) => self.insert_bool(a == b),
1360 (BinOp::Eq, a, b) if a == b => self.insert_bool(true),
1361 (BinOp::Ne, Left(a), Left(b)) => self.insert_bool(a != b),
1362 (BinOp::Ne, a, b) if a == b => self.insert_bool(false),
1363 _ => return None,
1364 };
1365
1366 if op.is_overflowing() {
1367 let false_val = self.insert_bool(false);
1368 Some(self.insert_tuple(vec![result, false_val]))
1369 } else {
1370 Some(result)
1371 }
1372 }
1373
1374 fn simplify_cast(
1375 &mut self,
1376 initial_kind: &mut CastKind,
1377 initial_operand: &mut Operand<'tcx>,
1378 to: Ty<'tcx>,
1379 location: Location,
1380 ) -> Option<VnIndex> {
1381 use CastKind::*;
1382 use rustc_middle::ty::adjustment::PointerCoercion::*;
1383
1384 let mut from = initial_operand.ty(self.local_decls, self.tcx);
1385 let mut kind = *initial_kind;
1386 let mut value = self.simplify_operand(initial_operand, location)?;
1387 if from == to {
1388 return Some(value);
1389 }
1390
1391 if let CastKind::PointerCoercion(ReifyFnPointer | ClosureFnPointer(_), _) = kind {
1392 return self.new_opaque();
1395 }
1396
1397 let mut was_ever_updated = false;
1398 loop {
1399 let mut was_updated_this_iteration = false;
1400
1401 if let Transmute = kind
1406 && from.is_raw_ptr()
1407 && to.is_raw_ptr()
1408 && self.pointers_have_same_metadata(from, to)
1409 {
1410 kind = PtrToPtr;
1411 was_updated_this_iteration = true;
1412 }
1413
1414 if let PtrToPtr = kind
1417 && let Value::Aggregate(AggregateTy::RawPtr { data_pointer_ty, .. }, _, fields) =
1418 self.get(value)
1419 && let ty::RawPtr(to_pointee, _) = to.kind()
1420 && to_pointee.is_sized(self.tcx, self.typing_env())
1421 {
1422 from = *data_pointer_ty;
1423 value = fields[0];
1424 was_updated_this_iteration = true;
1425 if *data_pointer_ty == to {
1426 return Some(fields[0]);
1427 }
1428 }
1429
1430 if let Transmute = kind
1433 && let Value::Aggregate(_aggregate_ty, variant_idx, field_values) = self.get(value)
1434 && let Some((field_idx, field_ty)) =
1435 self.value_is_all_in_one_field(from, *variant_idx)
1436 {
1437 from = field_ty;
1438 value = field_values[field_idx.as_usize()];
1439 was_updated_this_iteration = true;
1440 if field_ty == to {
1441 return Some(value);
1442 }
1443 }
1444
1445 if let Value::Cast {
1447 kind: inner_kind,
1448 value: inner_value,
1449 from: inner_from,
1450 to: inner_to,
1451 } = *self.get(value)
1452 {
1453 let new_kind = match (inner_kind, kind) {
1454 (PtrToPtr, PtrToPtr) => Some(PtrToPtr),
1458 (PtrToPtr, Transmute)
1462 if self.pointers_have_same_metadata(inner_from, inner_to) =>
1463 {
1464 Some(Transmute)
1465 }
1466 (Transmute, PtrToPtr) if self.pointers_have_same_metadata(from, to) => {
1469 Some(Transmute)
1470 }
1471 (Transmute, Transmute)
1474 if !self.type_may_have_niche_of_interest_to_backend(inner_to) =>
1475 {
1476 Some(Transmute)
1477 }
1478 _ => None,
1479 };
1480 if let Some(new_kind) = new_kind {
1481 kind = new_kind;
1482 from = inner_from;
1483 value = inner_value;
1484 was_updated_this_iteration = true;
1485 if inner_from == to {
1486 return Some(inner_value);
1487 }
1488 }
1489 }
1490
1491 if was_updated_this_iteration {
1492 was_ever_updated = true;
1493 } else {
1494 break;
1495 }
1496 }
1497
1498 if was_ever_updated && let Some(op) = self.try_as_operand(value, location) {
1499 *initial_operand = op;
1500 *initial_kind = kind;
1501 }
1502
1503 Some(self.insert(Value::Cast { kind, value, from, to }))
1504 }
1505
1506 fn simplify_len(&mut self, place: &mut Place<'tcx>, location: Location) -> Option<VnIndex> {
1507 let place_ty = place.ty(self.local_decls, self.tcx).ty;
1509 if let ty::Array(_, len) = place_ty.kind() {
1510 return self.insert_constant(Const::Ty(self.tcx.types.usize, *len));
1511 }
1512
1513 let mut inner = self.simplify_place_value(place, location)?;
1514
1515 while let Value::Address { place: borrowed, .. } = self.get(inner)
1518 && let [PlaceElem::Deref] = borrowed.projection[..]
1519 && let Some(borrowed) = self.locals[borrowed.local]
1520 {
1521 inner = borrowed;
1522 }
1523
1524 if let Value::Cast { kind, from, to, .. } = self.get(inner)
1526 && let CastKind::PointerCoercion(ty::adjustment::PointerCoercion::Unsize, _) = kind
1527 && let Some(from) = from.builtin_deref(true)
1528 && let ty::Array(_, len) = from.kind()
1529 && let Some(to) = to.builtin_deref(true)
1530 && let ty::Slice(..) = to.kind()
1531 {
1532 return self.insert_constant(Const::Ty(self.tcx.types.usize, *len));
1533 }
1534
1535 Some(self.insert(Value::Len(inner)))
1537 }
1538
1539 fn pointers_have_same_metadata(&self, left_ptr_ty: Ty<'tcx>, right_ptr_ty: Ty<'tcx>) -> bool {
1540 let left_meta_ty = left_ptr_ty.pointee_metadata_ty_or_projection(self.tcx);
1541 let right_meta_ty = right_ptr_ty.pointee_metadata_ty_or_projection(self.tcx);
1542 if left_meta_ty == right_meta_ty {
1543 true
1544 } else if let Ok(left) =
1545 self.tcx.try_normalize_erasing_regions(self.typing_env(), left_meta_ty)
1546 && let Ok(right) =
1547 self.tcx.try_normalize_erasing_regions(self.typing_env(), right_meta_ty)
1548 {
1549 left == right
1550 } else {
1551 false
1552 }
1553 }
1554
1555 fn type_may_have_niche_of_interest_to_backend(&self, ty: Ty<'tcx>) -> bool {
1562 let Ok(layout) = self.ecx.layout_of(ty) else {
1563 return true;
1565 };
1566
1567 if layout.uninhabited {
1568 return true;
1569 }
1570
1571 match layout.backend_repr {
1572 BackendRepr::Scalar(a) => !a.is_always_valid(&self.ecx),
1573 BackendRepr::ScalarPair(a, b) => {
1574 !a.is_always_valid(&self.ecx) || !b.is_always_valid(&self.ecx)
1575 }
1576 BackendRepr::SimdVector { .. } | BackendRepr::Memory { .. } => false,
1577 }
1578 }
1579
1580 fn value_is_all_in_one_field(
1581 &self,
1582 ty: Ty<'tcx>,
1583 variant: VariantIdx,
1584 ) -> Option<(FieldIdx, Ty<'tcx>)> {
1585 if let Ok(layout) = self.ecx.layout_of(ty)
1586 && let abi::Variants::Single { index } = layout.variants
1587 && index == variant
1588 && let Some((field_idx, field_layout)) = layout.non_1zst_field(&self.ecx)
1589 && layout.size == field_layout.size
1590 {
1591 Some((FieldIdx::from_usize(field_idx), field_layout.ty))
1595 } else if let ty::Adt(adt, args) = ty.kind()
1596 && adt.is_struct()
1597 && adt.repr().transparent()
1598 && let [single_field] = adt.non_enum_variant().fields.raw.as_slice()
1599 {
1600 Some((FieldIdx::ZERO, single_field.ty(self.tcx, args)))
1601 } else {
1602 None
1603 }
1604 }
1605}
1606
1607fn op_to_prop_const<'tcx>(
1608 ecx: &mut InterpCx<'tcx, DummyMachine>,
1609 op: &OpTy<'tcx>,
1610) -> Option<ConstValue<'tcx>> {
1611 if op.layout.is_unsized() {
1613 return None;
1614 }
1615
1616 if op.layout.is_zst() {
1618 return Some(ConstValue::ZeroSized);
1619 }
1620
1621 if !matches!(op.layout.backend_repr, BackendRepr::Scalar(..) | BackendRepr::ScalarPair(..)) {
1624 return None;
1625 }
1626
1627 if let BackendRepr::Scalar(abi::Scalar::Initialized { .. }) = op.layout.backend_repr
1629 && let Some(scalar) = ecx.read_scalar(op).discard_err()
1630 {
1631 if !scalar.try_to_scalar_int().is_ok() {
1632 return None;
1636 }
1637 return Some(ConstValue::Scalar(scalar));
1638 }
1639
1640 if let Either::Left(mplace) = op.as_mplace_or_imm() {
1643 let (size, _align) = ecx.size_and_align_of_mplace(&mplace).discard_err()??;
1644
1645 let alloc_ref = ecx.get_ptr_alloc(mplace.ptr(), size).discard_err()??;
1649 if alloc_ref.has_provenance() {
1650 return None;
1651 }
1652
1653 let pointer = mplace.ptr().into_pointer_or_addr().ok()?;
1654 let (prov, offset) = pointer.into_parts();
1655 let alloc_id = prov.alloc_id();
1656 intern_const_alloc_for_constprop(ecx, alloc_id).discard_err()?;
1657
1658 if let GlobalAlloc::Memory(alloc) = ecx.tcx.global_alloc(alloc_id)
1662 && alloc.inner().align >= op.layout.align.abi
1665 {
1666 return Some(ConstValue::Indirect { alloc_id, offset });
1667 }
1668 }
1669
1670 let alloc_id =
1672 ecx.intern_with_temp_alloc(op.layout, |ecx, dest| ecx.copy_op(op, dest)).discard_err()?;
1673 let value = ConstValue::Indirect { alloc_id, offset: Size::ZERO };
1674
1675 if ecx.tcx.global_alloc(alloc_id).unwrap_memory().inner().provenance().ptrs().is_empty() {
1679 return Some(value);
1680 }
1681
1682 None
1683}
1684
1685impl<'tcx> VnState<'_, 'tcx> {
1686 fn try_as_operand(&mut self, index: VnIndex, location: Location) -> Option<Operand<'tcx>> {
1689 if let Some(const_) = self.try_as_constant(index) {
1690 Some(Operand::Constant(Box::new(const_)))
1691 } else if let Some(local) = self.try_as_local(index, location) {
1692 self.reused_locals.insert(local);
1693 Some(Operand::Copy(local.into()))
1694 } else {
1695 None
1696 }
1697 }
1698
1699 fn try_as_constant(&mut self, index: VnIndex) -> Option<ConstOperand<'tcx>> {
1701 if let Value::Constant { value, disambiguator: 0 } = *self.get(index) {
1705 debug_assert!(value.is_deterministic());
1706 return Some(ConstOperand { span: DUMMY_SP, user_ty: None, const_: value });
1707 }
1708
1709 let op = self.evaluated[index].as_ref()?;
1710 if op.layout.is_unsized() {
1711 return None;
1713 }
1714
1715 let value = op_to_prop_const(&mut self.ecx, op)?;
1716
1717 assert!(!value.may_have_provenance(self.tcx, op.layout.size));
1721
1722 let const_ = Const::Val(value, op.layout.ty);
1723 Some(ConstOperand { span: DUMMY_SP, user_ty: None, const_ })
1724 }
1725
1726 fn try_as_local(&mut self, index: VnIndex, loc: Location) -> Option<Local> {
1729 let other = self.rev_locals.get(index)?;
1730 other
1731 .iter()
1732 .find(|&&other| self.ssa.assignment_dominates(&self.dominators, other, loc))
1733 .copied()
1734 }
1735}
1736
1737impl<'tcx> MutVisitor<'tcx> for VnState<'_, 'tcx> {
1738 fn tcx(&self) -> TyCtxt<'tcx> {
1739 self.tcx
1740 }
1741
1742 fn visit_place(&mut self, place: &mut Place<'tcx>, _: PlaceContext, location: Location) {
1743 self.simplify_place_projection(place, location);
1744 }
1745
1746 fn visit_operand(&mut self, operand: &mut Operand<'tcx>, location: Location) {
1747 self.simplify_operand(operand, location);
1748 }
1749
1750 fn visit_statement(&mut self, stmt: &mut Statement<'tcx>, location: Location) {
1751 if let StatementKind::Assign(box (ref mut lhs, ref mut rvalue)) = stmt.kind {
1752 self.simplify_place_projection(lhs, location);
1753
1754 if matches!(rvalue, Rvalue::Use(Operand::Constant(_))) {
1756 return;
1757 }
1758
1759 let value = lhs
1760 .as_local()
1761 .and_then(|local| self.locals[local])
1762 .or_else(|| self.simplify_rvalue(rvalue, location));
1763 let Some(value) = value else { return };
1764
1765 if let Some(const_) = self.try_as_constant(value) {
1766 *rvalue = Rvalue::Use(Operand::Constant(Box::new(const_)));
1767 } else if let Some(local) = self.try_as_local(value, location)
1768 && *rvalue != Rvalue::Use(Operand::Move(local.into()))
1769 {
1770 *rvalue = Rvalue::Use(Operand::Copy(local.into()));
1771 self.reused_locals.insert(local);
1772 }
1773
1774 return;
1775 }
1776 self.super_statement(stmt, location);
1777 }
1778}
1779
1780struct StorageRemover<'tcx> {
1781 tcx: TyCtxt<'tcx>,
1782 reused_locals: DenseBitSet<Local>,
1783}
1784
1785impl<'tcx> MutVisitor<'tcx> for StorageRemover<'tcx> {
1786 fn tcx(&self) -> TyCtxt<'tcx> {
1787 self.tcx
1788 }
1789
1790 fn visit_operand(&mut self, operand: &mut Operand<'tcx>, _: Location) {
1791 if let Operand::Move(place) = *operand
1792 && !place.is_indirect_first_projection()
1793 && self.reused_locals.contains(place.local)
1794 {
1795 *operand = Operand::Copy(place);
1796 }
1797 }
1798
1799 fn visit_statement(&mut self, stmt: &mut Statement<'tcx>, loc: Location) {
1800 match stmt.kind {
1801 StatementKind::StorageLive(l) | StatementKind::StorageDead(l)
1803 if self.reused_locals.contains(l) =>
1804 {
1805 stmt.make_nop()
1806 }
1807 _ => self.super_statement(stmt, loc),
1808 }
1809 }
1810}