2023-04-09 11:15:48 +00:00
|
|
|
//! Global value numbering.
|
|
|
|
//!
|
|
|
|
//! MIR may contain repeated and/or redundant computations. The objective of this pass is to detect
|
|
|
|
//! such redundancies and re-use the already-computed result when possible.
|
|
|
|
//!
|
|
|
|
//! In a first pass, we compute a symbolic representation of values that are assigned to SSA
|
|
|
|
//! locals. This symbolic representation is defined by the `Value` enum. Each produced instance of
|
|
|
|
//! `Value` is interned as a `VnIndex`, which allows us to cheaply compute identical values.
|
|
|
|
//!
|
|
|
|
//! From those assignments, we construct a mapping `VnIndex -> Vec<(Local, Location)>` of available
|
|
|
|
//! values, the locals in which they are stored, and a the assignment location.
|
|
|
|
//!
|
|
|
|
//! In a second pass, we traverse all (non SSA) assignments `x = rvalue` and operands. For each
|
|
|
|
//! one, we compute the `VnIndex` of the rvalue. If this `VnIndex` is associated to a constant, we
|
|
|
|
//! replace the rvalue/operand by that constant. Otherwise, if there is an SSA local `y`
|
|
|
|
//! associated to this `VnIndex`, and if its definition location strictly dominates the assignment
|
|
|
|
//! to `x`, we replace the assignment by `x = y`.
|
|
|
|
//!
|
|
|
|
//! By opportunity, this pass simplifies some `Rvalue`s based on the accumulated knowledge.
|
|
|
|
//!
|
2023-05-13 22:05:24 +00:00
|
|
|
//! # Operational semantic
|
|
|
|
//!
|
|
|
|
//! Operationally, this pass attempts to prove bitwise equality between locals. Given this MIR:
|
|
|
|
//! ```ignore (MIR)
|
|
|
|
//! _a = some value // has VnIndex i
|
|
|
|
//! // some MIR
|
|
|
|
//! _b = some other value // also has VnIndex i
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! We consider it to be replacable by:
|
|
|
|
//! ```ignore (MIR)
|
|
|
|
//! _a = some value // has VnIndex i
|
|
|
|
//! // some MIR
|
|
|
|
//! _c = some other value // also has VnIndex i
|
|
|
|
//! assume(_a bitwise equal to _c) // follows from having the same VnIndex
|
|
|
|
//! _b = _a // follows from the `assume`
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! Which is simplifiable to:
|
|
|
|
//! ```ignore (MIR)
|
|
|
|
//! _a = some value // has VnIndex i
|
|
|
|
//! // some MIR
|
|
|
|
//! _b = _a
|
|
|
|
//! ```
|
|
|
|
//!
|
2023-04-09 11:15:48 +00:00
|
|
|
//! # Handling of references
|
|
|
|
//!
|
|
|
|
//! We handle references by assigning a different "provenance" index to each Ref/AddressOf rvalue.
|
|
|
|
//! This ensure that we do not spuriously merge borrows that should not be merged. Meanwhile, we
|
|
|
|
//! consider all the derefs of an immutable reference to a freeze type to give the same value:
|
|
|
|
//! ```ignore (MIR)
|
|
|
|
//! _a = *_b // _b is &Freeze
|
|
|
|
//! _c = *_b // replaced by _c = _a
|
|
|
|
//! ```
|
2023-10-14 12:12:54 +00:00
|
|
|
//!
|
|
|
|
//! # Determinism of constant propagation
|
|
|
|
//!
|
|
|
|
//! When registering a new `Value`, we attempt to opportunistically evaluate it as a constant.
|
|
|
|
//! The evaluated form is inserted in `evaluated` as an `OpTy` or `None` if evaluation failed.
|
|
|
|
//!
|
|
|
|
//! The difficulty is non-deterministic evaluation of MIR constants. Some `Const` can have
|
|
|
|
//! different runtime values each time they are evaluated. This is the case with
|
|
|
|
//! `Const::Slice` which have a new pointer each time they are evaluated, and constants that
|
|
|
|
//! contain a fn pointer (`AllocId` pointing to a `GlobalAlloc::Function`) pointing to a different
|
|
|
|
//! symbol in each codegen unit.
|
|
|
|
//!
|
|
|
|
//! Meanwhile, we want to be able to read indirect constants. For instance:
|
|
|
|
//! ```
|
|
|
|
//! static A: &'static &'static u8 = &&63;
|
|
|
|
//! fn foo() -> u8 {
|
|
|
|
//! **A // We want to replace by 63.
|
|
|
|
//! }
|
|
|
|
//! fn bar() -> u8 {
|
|
|
|
//! b"abc"[1] // We want to replace by 'b'.
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! The `Value::Constant` variant stores a possibly unevaluated constant. Evaluating that constant
|
|
|
|
//! may be non-deterministic. When that happens, we assign a disambiguator to ensure that we do not
|
|
|
|
//! merge the constants. See `duplicate_slice` test in `gvn.rs`.
|
|
|
|
//!
|
|
|
|
//! Second, when writing constants in MIR, we do not write `Const::Slice` or `Const`
|
|
|
|
//! that contain `AllocId`s.
|
2023-04-09 11:15:48 +00:00
|
|
|
|
2023-10-07 09:19:37 +00:00
|
|
|
use rustc_const_eval::interpret::{intern_const_alloc_for_constprop, MemoryKind};
|
2023-10-12 16:15:52 +00:00
|
|
|
use rustc_const_eval::interpret::{ImmTy, InterpCx, OpTy, Projectable, Scalar};
|
2023-05-03 17:27:10 +00:00
|
|
|
use rustc_data_structures::fx::FxIndexSet;
|
2023-03-20 18:05:07 +00:00
|
|
|
use rustc_data_structures::graph::dominators::Dominators;
|
2023-05-03 18:36:53 +00:00
|
|
|
use rustc_hir::def::DefKind;
|
2023-03-20 18:05:07 +00:00
|
|
|
use rustc_index::bit_set::BitSet;
|
2023-10-24 00:16:14 +00:00
|
|
|
use rustc_index::newtype_index;
|
2023-03-20 18:05:07 +00:00
|
|
|
use rustc_index::IndexVec;
|
2023-10-22 14:49:00 +00:00
|
|
|
use rustc_middle::mir::interpret::GlobalAlloc;
|
2023-03-20 18:05:07 +00:00
|
|
|
use rustc_middle::mir::visit::*;
|
|
|
|
use rustc_middle::mir::*;
|
2023-09-19 20:12:48 +00:00
|
|
|
use rustc_middle::ty::layout::LayoutOf;
|
|
|
|
use rustc_middle::ty::{self, Ty, TyCtxt, TypeAndMut};
|
2023-05-03 18:36:53 +00:00
|
|
|
use rustc_span::def_id::DefId;
|
2023-09-19 20:12:48 +00:00
|
|
|
use rustc_span::DUMMY_SP;
|
|
|
|
use rustc_target::abi::{self, Abi, Size, VariantIdx, FIRST_VARIANT};
|
2023-05-16 10:19:08 +00:00
|
|
|
use smallvec::SmallVec;
|
2023-09-16 09:16:04 +00:00
|
|
|
use std::borrow::Cow;
|
2023-03-20 18:05:07 +00:00
|
|
|
|
2023-09-19 20:12:48 +00:00
|
|
|
use crate::dataflow_const_prop::DummyMachine;
|
2023-07-20 17:03:44 +00:00
|
|
|
use crate::ssa::{AssignedValue, SsaLocals};
|
2023-09-19 20:12:48 +00:00
|
|
|
use either::Either;
|
2023-03-20 18:05:07 +00:00
|
|
|
|
|
|
|
pub struct GVN;
|
|
|
|
|
|
|
|
impl<'tcx> MirPass<'tcx> for GVN {
|
|
|
|
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
|
2023-05-21 10:33:03 +00:00
|
|
|
sess.mir_opt_level() >= 2
|
2023-03-20 18:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[instrument(level = "trace", skip(self, tcx, body))]
|
|
|
|
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
|
|
|
|
debug!(def_id = ?body.source.def_id());
|
|
|
|
propagate_ssa(tcx, body);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn propagate_ssa<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
|
|
|
|
let param_env = tcx.param_env_reveal_all_normalized(body.source.def_id());
|
|
|
|
let ssa = SsaLocals::new(body);
|
|
|
|
// Clone dominators as we need them while mutating the body.
|
|
|
|
let dominators = body.basic_blocks.dominators().clone();
|
|
|
|
|
2023-04-12 18:39:56 +00:00
|
|
|
let mut state = VnState::new(tcx, param_env, &ssa, &dominators, &body.local_decls);
|
2023-07-20 17:03:44 +00:00
|
|
|
ssa.for_each_assignment_mut(
|
|
|
|
body.basic_blocks.as_mut_preserves_cfg(),
|
|
|
|
|local, value, location| {
|
|
|
|
let value = match value {
|
|
|
|
// We do not know anything of this assigned value.
|
|
|
|
AssignedValue::Arg | AssignedValue::Terminator(_) => None,
|
|
|
|
// Try to get some insight.
|
|
|
|
AssignedValue::Rvalue(rvalue) => {
|
|
|
|
let value = state.simplify_rvalue(rvalue, location);
|
|
|
|
// FIXME(#112651) `rvalue` may have a subtype to `local`. We can only mark `local` as
|
|
|
|
// reusable if we have an exact type match.
|
|
|
|
if state.local_decls[local].ty != rvalue.ty(state.local_decls, tcx) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
value
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// `next_opaque` is `Some`, so `new_opaque` must return `Some`.
|
|
|
|
let value = value.or_else(|| state.new_opaque()).unwrap();
|
2023-07-01 14:57:29 +00:00
|
|
|
state.assign(local, value);
|
2023-07-20 17:03:44 +00:00
|
|
|
},
|
|
|
|
);
|
2023-03-20 18:05:07 +00:00
|
|
|
|
|
|
|
// Stop creating opaques during replacement as it is useless.
|
|
|
|
state.next_opaque = None;
|
|
|
|
|
|
|
|
let reverse_postorder = body.basic_blocks.reverse_postorder().to_vec();
|
|
|
|
for bb in reverse_postorder {
|
|
|
|
let data = &mut body.basic_blocks.as_mut_preserves_cfg()[bb];
|
2023-04-12 18:39:56 +00:00
|
|
|
state.visit_basic_block_data(bb, data);
|
2023-03-20 18:05:07 +00:00
|
|
|
}
|
|
|
|
|
2023-04-09 11:15:48 +00:00
|
|
|
// For each local that is reused (`y` above), we remove its storage statements do avoid any
|
|
|
|
// difficulty. Those locals are SSA, so should be easy to optimize by LLVM without storage
|
|
|
|
// statements.
|
2023-04-12 18:39:56 +00:00
|
|
|
StorageRemover { tcx, reused_locals: state.reused_locals }.visit_body_preserves_cfg(body);
|
2023-03-20 18:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
newtype_index! {
|
|
|
|
struct VnIndex {}
|
|
|
|
}
|
|
|
|
|
2023-05-03 18:36:53 +00:00
|
|
|
/// Computing the aggregate's type can be quite slow, so we only keep the minimal amount of
|
|
|
|
/// information to reconstruct it when needed.
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
|
|
|
|
enum AggregateTy<'tcx> {
|
|
|
|
/// Invariant: this must not be used for an empty array.
|
|
|
|
Array,
|
|
|
|
Tuple,
|
|
|
|
Def(DefId, ty::GenericArgsRef<'tcx>),
|
|
|
|
}
|
|
|
|
|
2023-09-19 20:12:48 +00:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
|
|
|
|
enum AddressKind {
|
|
|
|
Ref(BorrowKind),
|
|
|
|
Address(Mutability),
|
|
|
|
}
|
|
|
|
|
2023-03-20 18:05:07 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq, Hash)]
|
|
|
|
enum Value<'tcx> {
|
|
|
|
// Root values.
|
|
|
|
/// Used to represent values we know nothing about.
|
|
|
|
/// The `usize` is a counter incremented by `new_opaque`.
|
|
|
|
Opaque(usize),
|
|
|
|
/// Evaluated or unevaluated constant value.
|
2023-10-14 12:12:54 +00:00
|
|
|
Constant {
|
|
|
|
value: Const<'tcx>,
|
|
|
|
/// Some constants do not have a deterministic value. To avoid merging two instances of the
|
|
|
|
/// same `Const`, we assign them an additional integer index.
|
|
|
|
disambiguator: usize,
|
|
|
|
},
|
2023-03-20 18:05:07 +00:00
|
|
|
/// An aggregate value, either tuple/closure/struct/enum.
|
|
|
|
/// This does not contain unions, as we cannot reason with the value.
|
2023-05-03 18:36:53 +00:00
|
|
|
Aggregate(AggregateTy<'tcx>, VariantIdx, Vec<VnIndex>),
|
2023-03-20 18:05:07 +00:00
|
|
|
/// This corresponds to a `[value; count]` expression.
|
|
|
|
Repeat(VnIndex, ty::Const<'tcx>),
|
|
|
|
/// The address of a place.
|
|
|
|
Address {
|
|
|
|
place: Place<'tcx>,
|
2023-09-19 20:12:48 +00:00
|
|
|
kind: AddressKind,
|
2023-03-20 18:05:07 +00:00
|
|
|
/// Give each borrow and pointer a different provenance, so we don't merge them.
|
|
|
|
provenance: usize,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Extractions.
|
|
|
|
/// This is the *value* obtained by projecting another value.
|
|
|
|
Projection(VnIndex, ProjectionElem<VnIndex, Ty<'tcx>>),
|
|
|
|
/// Discriminant of the given value.
|
|
|
|
Discriminant(VnIndex),
|
|
|
|
/// Length of an array or slice.
|
|
|
|
Len(VnIndex),
|
|
|
|
|
|
|
|
// Operations.
|
|
|
|
NullaryOp(NullOp<'tcx>, Ty<'tcx>),
|
|
|
|
UnaryOp(UnOp, VnIndex),
|
|
|
|
BinaryOp(BinOp, VnIndex, VnIndex),
|
|
|
|
CheckedBinaryOp(BinOp, VnIndex, VnIndex),
|
|
|
|
Cast {
|
|
|
|
kind: CastKind,
|
|
|
|
value: VnIndex,
|
|
|
|
from: Ty<'tcx>,
|
|
|
|
to: Ty<'tcx>,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
struct VnState<'body, 'tcx> {
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2023-09-19 20:12:48 +00:00
|
|
|
ecx: InterpCx<'tcx, 'tcx, DummyMachine>,
|
2023-03-20 18:05:07 +00:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
local_decls: &'body LocalDecls<'tcx>,
|
|
|
|
/// Value stored in each local.
|
|
|
|
locals: IndexVec<Local, Option<VnIndex>>,
|
2023-05-03 17:27:10 +00:00
|
|
|
/// Locals that are assigned that value.
|
|
|
|
// This vector does not hold all the values of `VnIndex` that we create.
|
|
|
|
// It stops at the largest value created in the first phase of collecting assignments.
|
2023-05-16 10:19:08 +00:00
|
|
|
rev_locals: IndexVec<VnIndex, SmallVec<[Local; 1]>>,
|
2023-03-20 18:05:07 +00:00
|
|
|
values: FxIndexSet<Value<'tcx>>,
|
2023-09-19 20:12:48 +00:00
|
|
|
/// Values evaluated as constants if possible.
|
|
|
|
evaluated: IndexVec<VnIndex, Option<OpTy<'tcx>>>,
|
2023-03-20 18:05:07 +00:00
|
|
|
/// Counter to generate different values.
|
|
|
|
/// This is an option to stop creating opaques during replacement.
|
|
|
|
next_opaque: Option<usize>,
|
2023-05-16 10:19:08 +00:00
|
|
|
/// Cache the value of the `unsized_locals` features, to avoid fetching it repeatedly in a loop.
|
|
|
|
feature_unsized_locals: bool,
|
2023-04-12 18:39:56 +00:00
|
|
|
ssa: &'body SsaLocals,
|
|
|
|
dominators: &'body Dominators<BasicBlock>,
|
|
|
|
reused_locals: BitSet<Local>,
|
2023-03-20 18:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'body, 'tcx> VnState<'body, 'tcx> {
|
|
|
|
fn new(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2023-04-12 18:39:56 +00:00
|
|
|
ssa: &'body SsaLocals,
|
|
|
|
dominators: &'body Dominators<BasicBlock>,
|
2023-03-20 18:05:07 +00:00
|
|
|
local_decls: &'body LocalDecls<'tcx>,
|
|
|
|
) -> Self {
|
|
|
|
VnState {
|
|
|
|
tcx,
|
2023-09-19 20:12:48 +00:00
|
|
|
ecx: InterpCx::new(tcx, DUMMY_SP, param_env, DummyMachine),
|
2023-03-20 18:05:07 +00:00
|
|
|
param_env,
|
|
|
|
local_decls,
|
|
|
|
locals: IndexVec::from_elem(None, local_decls),
|
2023-05-03 17:27:10 +00:00
|
|
|
rev_locals: IndexVec::default(),
|
2023-03-20 18:05:07 +00:00
|
|
|
values: FxIndexSet::default(),
|
2023-09-19 20:12:48 +00:00
|
|
|
evaluated: IndexVec::new(),
|
2023-03-20 18:05:07 +00:00
|
|
|
next_opaque: Some(0),
|
2023-05-16 10:19:08 +00:00
|
|
|
feature_unsized_locals: tcx.features().unsized_locals,
|
2023-04-12 18:39:56 +00:00
|
|
|
ssa,
|
|
|
|
dominators,
|
|
|
|
reused_locals: BitSet::new_empty(local_decls.len()),
|
2023-03-20 18:05:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[instrument(level = "trace", skip(self), ret)]
|
|
|
|
fn insert(&mut self, value: Value<'tcx>) -> VnIndex {
|
2023-09-19 20:12:48 +00:00
|
|
|
let (index, new) = self.values.insert_full(value);
|
|
|
|
let index = VnIndex::from_usize(index);
|
|
|
|
if new {
|
|
|
|
let evaluated = self.eval_to_const(index);
|
|
|
|
let _index = self.evaluated.push(evaluated);
|
|
|
|
debug_assert_eq!(index, _index);
|
|
|
|
}
|
|
|
|
index
|
2023-03-20 18:05:07 +00:00
|
|
|
}
|
|
|
|
|
2023-04-09 11:15:48 +00:00
|
|
|
/// Create a new `Value` for which we have no information at all, except that it is distinct
|
|
|
|
/// from all the others.
|
2023-03-20 18:05:07 +00:00
|
|
|
#[instrument(level = "trace", skip(self), ret)]
|
|
|
|
fn new_opaque(&mut self) -> Option<VnIndex> {
|
|
|
|
let next_opaque = self.next_opaque.as_mut()?;
|
|
|
|
let value = Value::Opaque(*next_opaque);
|
|
|
|
*next_opaque += 1;
|
|
|
|
Some(self.insert(value))
|
|
|
|
}
|
|
|
|
|
2023-04-09 11:15:48 +00:00
|
|
|
/// Create a new `Value::Address` distinct from all the others.
|
2023-03-20 18:05:07 +00:00
|
|
|
#[instrument(level = "trace", skip(self), ret)]
|
2023-09-19 20:12:48 +00:00
|
|
|
fn new_pointer(&mut self, place: Place<'tcx>, kind: AddressKind) -> Option<VnIndex> {
|
2023-03-20 18:05:07 +00:00
|
|
|
let next_opaque = self.next_opaque.as_mut()?;
|
2023-09-19 20:12:48 +00:00
|
|
|
let value = Value::Address { place, kind, provenance: *next_opaque };
|
2023-03-20 18:05:07 +00:00
|
|
|
*next_opaque += 1;
|
|
|
|
Some(self.insert(value))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get(&self, index: VnIndex) -> &Value<'tcx> {
|
|
|
|
self.values.get_index(index.as_usize()).unwrap()
|
|
|
|
}
|
|
|
|
|
2023-04-09 11:15:48 +00:00
|
|
|
/// Record that `local` is assigned `value`. `local` must be SSA.
|
2023-03-20 18:05:07 +00:00
|
|
|
#[instrument(level = "trace", skip(self))]
|
|
|
|
fn assign(&mut self, local: Local, value: VnIndex) {
|
|
|
|
self.locals[local] = Some(value);
|
2023-05-01 09:59:00 +00:00
|
|
|
|
|
|
|
// Only register the value if its type is `Sized`, as we will emit copies of it.
|
2023-05-16 10:19:08 +00:00
|
|
|
let is_sized = !self.feature_unsized_locals
|
2023-05-01 09:59:00 +00:00
|
|
|
|| self.local_decls[local].ty.is_sized(self.tcx, self.param_env);
|
|
|
|
if is_sized {
|
2023-05-16 10:19:08 +00:00
|
|
|
self.rev_locals.ensure_contains_elem(value, SmallVec::new);
|
2023-05-03 17:27:10 +00:00
|
|
|
self.rev_locals[value].push(local);
|
2023-05-01 09:59:00 +00:00
|
|
|
}
|
2023-03-20 18:05:07 +00:00
|
|
|
}
|
|
|
|
|
2023-10-14 12:12:54 +00:00
|
|
|
fn insert_constant(&mut self, value: Const<'tcx>) -> Option<VnIndex> {
|
|
|
|
let disambiguator = if value.is_deterministic() {
|
|
|
|
// The constant is deterministic, no need to disambiguate.
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
// Multiple mentions of this constant will yield different values,
|
|
|
|
// so assign a different `disambiguator` to ensure they do not get the same `VnIndex`.
|
|
|
|
let next_opaque = self.next_opaque.as_mut()?;
|
|
|
|
let disambiguator = *next_opaque;
|
|
|
|
*next_opaque += 1;
|
|
|
|
disambiguator
|
|
|
|
};
|
|
|
|
Some(self.insert(Value::Constant { value, disambiguator }))
|
|
|
|
}
|
|
|
|
|
2023-03-20 21:37:36 +00:00
|
|
|
fn insert_bool(&mut self, flag: bool) -> VnIndex {
|
|
|
|
// Booleans are deterministic.
|
|
|
|
self.insert(Value::Constant { value: Const::from_bool(self.tcx, flag), disambiguator: 0 })
|
|
|
|
}
|
|
|
|
|
2023-09-16 09:35:46 +00:00
|
|
|
fn insert_scalar(&mut self, scalar: Scalar, ty: Ty<'tcx>) -> VnIndex {
|
2023-10-14 12:12:54 +00:00
|
|
|
self.insert_constant(Const::from_scalar(self.tcx, scalar, ty))
|
|
|
|
.expect("scalars are deterministic")
|
2023-09-16 09:35:46 +00:00
|
|
|
}
|
|
|
|
|
2023-03-20 21:37:36 +00:00
|
|
|
fn insert_tuple(&mut self, values: Vec<VnIndex>) -> VnIndex {
|
|
|
|
self.insert(Value::Aggregate(AggregateTy::Tuple, VariantIdx::from_u32(0), values))
|
|
|
|
}
|
|
|
|
|
2023-09-19 20:12:48 +00:00
|
|
|
#[instrument(level = "trace", skip(self), ret)]
|
|
|
|
fn eval_to_const(&mut self, value: VnIndex) -> Option<OpTy<'tcx>> {
|
|
|
|
use Value::*;
|
|
|
|
let op = match *self.get(value) {
|
|
|
|
Opaque(_) => return None,
|
|
|
|
// Do not bother evaluating repeat expressions. This would uselessly consume memory.
|
|
|
|
Repeat(..) => return None,
|
|
|
|
|
2023-10-14 12:12:54 +00:00
|
|
|
Constant { ref value, disambiguator: _ } => {
|
|
|
|
self.ecx.eval_mir_constant(value, None, None).ok()?
|
|
|
|
}
|
2023-05-03 18:36:53 +00:00
|
|
|
Aggregate(kind, variant, ref fields) => {
|
2023-09-19 20:12:48 +00:00
|
|
|
let fields = fields
|
|
|
|
.iter()
|
|
|
|
.map(|&f| self.evaluated[f].as_ref())
|
|
|
|
.collect::<Option<Vec<_>>>()?;
|
2023-05-03 18:36:53 +00:00
|
|
|
let ty = match kind {
|
|
|
|
AggregateTy::Array => {
|
|
|
|
assert!(fields.len() > 0);
|
|
|
|
Ty::new_array(self.tcx, fields[0].layout.ty, fields.len() as u64)
|
|
|
|
}
|
|
|
|
AggregateTy::Tuple => {
|
|
|
|
Ty::new_tup_from_iter(self.tcx, fields.iter().map(|f| f.layout.ty))
|
|
|
|
}
|
|
|
|
AggregateTy::Def(def_id, args) => {
|
|
|
|
self.tcx.type_of(def_id).instantiate(self.tcx, args)
|
|
|
|
}
|
|
|
|
};
|
2023-09-19 20:12:48 +00:00
|
|
|
let variant = if ty.is_enum() { Some(variant) } else { None };
|
|
|
|
let ty = self.ecx.layout_of(ty).ok()?;
|
2023-09-23 14:55:28 +00:00
|
|
|
if ty.is_zst() {
|
|
|
|
ImmTy::uninit(ty).into()
|
|
|
|
} else if matches!(ty.abi, Abi::Scalar(..) | Abi::ScalarPair(..)) {
|
2023-10-03 18:06:47 +00:00
|
|
|
let dest = self.ecx.allocate(ty, MemoryKind::Stack).ok()?;
|
|
|
|
let variant_dest = if let Some(variant) = variant {
|
|
|
|
self.ecx.project_downcast(&dest, variant).ok()?
|
|
|
|
} else {
|
|
|
|
dest.clone()
|
|
|
|
};
|
|
|
|
for (field_index, op) in fields.into_iter().enumerate() {
|
|
|
|
let field_dest = self.ecx.project_field(&variant_dest, field_index).ok()?;
|
|
|
|
self.ecx.copy_op(op, &field_dest, /*allow_transmute*/ false).ok()?;
|
|
|
|
}
|
|
|
|
self.ecx.write_discriminant(variant.unwrap_or(FIRST_VARIANT), &dest).ok()?;
|
2023-11-25 18:41:53 +01:00
|
|
|
self.ecx
|
|
|
|
.alloc_mark_immutable(dest.ptr().provenance.unwrap().alloc_id())
|
|
|
|
.ok()?;
|
2023-10-03 18:06:47 +00:00
|
|
|
dest.into()
|
2023-09-23 14:55:28 +00:00
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
}
|
2023-09-19 20:12:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Projection(base, elem) => {
|
|
|
|
let value = self.evaluated[base].as_ref()?;
|
|
|
|
let elem = match elem {
|
|
|
|
ProjectionElem::Deref => ProjectionElem::Deref,
|
|
|
|
ProjectionElem::Downcast(name, read_variant) => {
|
|
|
|
ProjectionElem::Downcast(name, read_variant)
|
|
|
|
}
|
|
|
|
ProjectionElem::Field(f, ty) => ProjectionElem::Field(f, ty),
|
|
|
|
ProjectionElem::ConstantIndex { offset, min_length, from_end } => {
|
|
|
|
ProjectionElem::ConstantIndex { offset, min_length, from_end }
|
|
|
|
}
|
|
|
|
ProjectionElem::Subslice { from, to, from_end } => {
|
|
|
|
ProjectionElem::Subslice { from, to, from_end }
|
|
|
|
}
|
|
|
|
ProjectionElem::OpaqueCast(ty) => ProjectionElem::OpaqueCast(ty),
|
|
|
|
ProjectionElem::Subtype(ty) => ProjectionElem::Subtype(ty),
|
|
|
|
// This should have been replaced by a `ConstantIndex` earlier.
|
|
|
|
ProjectionElem::Index(_) => return None,
|
|
|
|
};
|
|
|
|
self.ecx.project(value, elem).ok()?
|
|
|
|
}
|
|
|
|
Address { place, kind, provenance: _ } => {
|
|
|
|
if !place.is_indirect_first_projection() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let local = self.locals[place.local]?;
|
|
|
|
let pointer = self.evaluated[local].as_ref()?;
|
|
|
|
let mut mplace = self.ecx.deref_pointer(pointer).ok()?;
|
|
|
|
for proj in place.projection.iter().skip(1) {
|
|
|
|
// We have no call stack to associate a local with a value, so we cannot interpret indexing.
|
|
|
|
if matches!(proj, ProjectionElem::Index(_)) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
mplace = self.ecx.project(&mplace, proj).ok()?;
|
|
|
|
}
|
|
|
|
let pointer = mplace.to_ref(&self.ecx);
|
|
|
|
let ty = match kind {
|
|
|
|
AddressKind::Ref(bk) => Ty::new_ref(
|
|
|
|
self.tcx,
|
|
|
|
self.tcx.lifetimes.re_erased,
|
|
|
|
ty::TypeAndMut { ty: mplace.layout.ty, mutbl: bk.to_mutbl_lossy() },
|
|
|
|
),
|
|
|
|
AddressKind::Address(mutbl) => {
|
|
|
|
Ty::new_ptr(self.tcx, TypeAndMut { ty: mplace.layout.ty, mutbl })
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let layout = self.ecx.layout_of(ty).ok()?;
|
|
|
|
ImmTy::from_immediate(pointer, layout).into()
|
|
|
|
}
|
|
|
|
|
|
|
|
Discriminant(base) => {
|
|
|
|
let base = self.evaluated[base].as_ref()?;
|
|
|
|
let variant = self.ecx.read_discriminant(base).ok()?;
|
|
|
|
let discr_value =
|
|
|
|
self.ecx.discriminant_for_variant(base.layout.ty, variant).ok()?;
|
|
|
|
discr_value.into()
|
|
|
|
}
|
|
|
|
Len(slice) => {
|
|
|
|
let slice = self.evaluated[slice].as_ref()?;
|
|
|
|
let usize_layout = self.ecx.layout_of(self.tcx.types.usize).unwrap();
|
|
|
|
let len = slice.len(&self.ecx).ok()?;
|
|
|
|
let imm = ImmTy::try_from_uint(len, usize_layout)?;
|
|
|
|
imm.into()
|
|
|
|
}
|
|
|
|
NullaryOp(null_op, ty) => {
|
|
|
|
let layout = self.ecx.layout_of(ty).ok()?;
|
2023-11-13 08:24:55 -05:00
|
|
|
if let NullOp::SizeOf | NullOp::AlignOf = null_op
|
|
|
|
&& layout.is_unsized()
|
|
|
|
{
|
2023-09-19 20:12:48 +00:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let val = match null_op {
|
|
|
|
NullOp::SizeOf => layout.size.bytes(),
|
|
|
|
NullOp::AlignOf => layout.align.abi.bytes(),
|
2023-10-31 23:41:40 +00:00
|
|
|
NullOp::OffsetOf(fields) => {
|
|
|
|
layout.offset_of_subfield(&self.ecx, fields.iter()).bytes()
|
|
|
|
}
|
2024-02-07 10:26:00 -05:00
|
|
|
NullOp::DebugAssertions => return None,
|
2023-09-19 20:12:48 +00:00
|
|
|
};
|
|
|
|
let usize_layout = self.ecx.layout_of(self.tcx.types.usize).unwrap();
|
|
|
|
let imm = ImmTy::try_from_uint(val, usize_layout)?;
|
|
|
|
imm.into()
|
|
|
|
}
|
|
|
|
UnaryOp(un_op, operand) => {
|
|
|
|
let operand = self.evaluated[operand].as_ref()?;
|
|
|
|
let operand = self.ecx.read_immediate(operand).ok()?;
|
|
|
|
let (val, _) = self.ecx.overflowing_unary_op(un_op, &operand).ok()?;
|
|
|
|
val.into()
|
|
|
|
}
|
|
|
|
BinaryOp(bin_op, lhs, rhs) => {
|
|
|
|
let lhs = self.evaluated[lhs].as_ref()?;
|
|
|
|
let lhs = self.ecx.read_immediate(lhs).ok()?;
|
|
|
|
let rhs = self.evaluated[rhs].as_ref()?;
|
|
|
|
let rhs = self.ecx.read_immediate(rhs).ok()?;
|
|
|
|
let (val, _) = self.ecx.overflowing_binary_op(bin_op, &lhs, &rhs).ok()?;
|
|
|
|
val.into()
|
|
|
|
}
|
|
|
|
CheckedBinaryOp(bin_op, lhs, rhs) => {
|
|
|
|
let lhs = self.evaluated[lhs].as_ref()?;
|
|
|
|
let lhs = self.ecx.read_immediate(lhs).ok()?;
|
|
|
|
let rhs = self.evaluated[rhs].as_ref()?;
|
|
|
|
let rhs = self.ecx.read_immediate(rhs).ok()?;
|
|
|
|
let (val, overflowed) = self.ecx.overflowing_binary_op(bin_op, &lhs, &rhs).ok()?;
|
|
|
|
let tuple = Ty::new_tup_from_iter(
|
|
|
|
self.tcx,
|
|
|
|
[val.layout.ty, self.tcx.types.bool].into_iter(),
|
|
|
|
);
|
|
|
|
let tuple = self.ecx.layout_of(tuple).ok()?;
|
|
|
|
ImmTy::from_scalar_pair(val.to_scalar(), Scalar::from_bool(overflowed), tuple)
|
|
|
|
.into()
|
|
|
|
}
|
|
|
|
Cast { kind, value, from: _, to } => match kind {
|
|
|
|
CastKind::IntToInt | CastKind::IntToFloat => {
|
|
|
|
let value = self.evaluated[value].as_ref()?;
|
|
|
|
let value = self.ecx.read_immediate(value).ok()?;
|
|
|
|
let to = self.ecx.layout_of(to).ok()?;
|
|
|
|
let res = self.ecx.int_to_int_or_float(&value, to).ok()?;
|
|
|
|
res.into()
|
|
|
|
}
|
|
|
|
CastKind::FloatToFloat | CastKind::FloatToInt => {
|
|
|
|
let value = self.evaluated[value].as_ref()?;
|
|
|
|
let value = self.ecx.read_immediate(value).ok()?;
|
|
|
|
let to = self.ecx.layout_of(to).ok()?;
|
|
|
|
let res = self.ecx.float_to_float_or_int(&value, to).ok()?;
|
|
|
|
res.into()
|
|
|
|
}
|
|
|
|
CastKind::Transmute => {
|
|
|
|
let value = self.evaluated[value].as_ref()?;
|
|
|
|
let to = self.ecx.layout_of(to).ok()?;
|
2023-09-23 07:23:14 +00:00
|
|
|
// `offset` for immediates only supports scalar/scalar-pair ABIs,
|
|
|
|
// so bail out if the target is not one.
|
|
|
|
if value.as_mplace_or_imm().is_right() {
|
2023-10-12 06:35:54 +00:00
|
|
|
match (value.layout.abi, to.abi) {
|
|
|
|
(Abi::Scalar(..), Abi::Scalar(..)) => {}
|
|
|
|
(Abi::ScalarPair(..), Abi::ScalarPair(..)) => {}
|
2023-09-23 07:23:14 +00:00
|
|
|
_ => return None,
|
|
|
|
}
|
|
|
|
}
|
2023-09-19 20:12:48 +00:00
|
|
|
value.offset(Size::ZERO, to, &self.ecx).ok()?
|
|
|
|
}
|
2023-12-17 22:30:20 +00:00
|
|
|
CastKind::PointerCoercion(ty::adjustment::PointerCoercion::Unsize) => {
|
|
|
|
let src = self.evaluated[value].as_ref()?;
|
|
|
|
let to = self.ecx.layout_of(to).ok()?;
|
|
|
|
let dest = self.ecx.allocate(to, MemoryKind::Stack).ok()?;
|
|
|
|
self.ecx.unsize_into(src, to, &dest.clone().into()).ok()?;
|
|
|
|
self.ecx
|
|
|
|
.alloc_mark_immutable(dest.ptr().provenance.unwrap().alloc_id())
|
|
|
|
.ok()?;
|
|
|
|
dest.into()
|
|
|
|
}
|
2024-02-13 17:19:30 +00:00
|
|
|
CastKind::FnPtrToPtr | CastKind::PtrToPtr => {
|
|
|
|
let src = self.evaluated[value].as_ref()?;
|
|
|
|
let src = self.ecx.read_immediate(src).ok()?;
|
|
|
|
let to = self.ecx.layout_of(to).ok()?;
|
|
|
|
let ret = self.ecx.ptr_to_ptr(&src, to).ok()?;
|
|
|
|
ret.into()
|
|
|
|
}
|
|
|
|
CastKind::PointerCoercion(
|
2024-01-06 02:36:25 +00:00
|
|
|
ty::adjustment::PointerCoercion::MutToConstPointer
|
|
|
|
| ty::adjustment::PointerCoercion::ArrayToPointer
|
|
|
|
| ty::adjustment::PointerCoercion::UnsafeFnPointer,
|
|
|
|
) => {
|
|
|
|
let src = self.evaluated[value].as_ref()?;
|
|
|
|
let src = self.ecx.read_immediate(src).ok()?;
|
|
|
|
let to = self.ecx.layout_of(to).ok()?;
|
2024-02-13 17:19:30 +00:00
|
|
|
ImmTy::from_immediate(*src, to).into()
|
2024-01-06 02:36:25 +00:00
|
|
|
}
|
2023-09-19 20:12:48 +00:00
|
|
|
_ => return None,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
Some(op)
|
|
|
|
}
|
|
|
|
|
2023-09-16 09:16:04 +00:00
|
|
|
fn project(
|
|
|
|
&mut self,
|
|
|
|
place: PlaceRef<'tcx>,
|
|
|
|
value: VnIndex,
|
|
|
|
proj: PlaceElem<'tcx>,
|
|
|
|
) -> Option<VnIndex> {
|
|
|
|
let proj = match proj {
|
|
|
|
ProjectionElem::Deref => {
|
|
|
|
let ty = place.ty(self.local_decls, self.tcx).ty;
|
|
|
|
if let Some(Mutability::Not) = ty.ref_mutability()
|
|
|
|
&& let Some(pointee_ty) = ty.builtin_deref(true)
|
|
|
|
&& pointee_ty.ty.is_freeze(self.tcx, self.param_env)
|
|
|
|
{
|
|
|
|
// An immutable borrow `_x` always points to the same value for the
|
|
|
|
// lifetime of the borrow, so we can merge all instances of `*_x`.
|
|
|
|
ProjectionElem::Deref
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ProjectionElem::Downcast(name, index) => ProjectionElem::Downcast(name, index),
|
2023-09-16 09:35:46 +00:00
|
|
|
ProjectionElem::Field(f, ty) => {
|
|
|
|
if let Value::Aggregate(_, _, fields) = self.get(value) {
|
|
|
|
return Some(fields[f.as_usize()]);
|
|
|
|
} else if let Value::Projection(outer_value, ProjectionElem::Downcast(_, read_variant)) = self.get(value)
|
|
|
|
&& let Value::Aggregate(_, written_variant, fields) = self.get(*outer_value)
|
2023-10-11 16:11:55 +00:00
|
|
|
// This pass is not aware of control-flow, so we do not know whether the
|
|
|
|
// replacement we are doing is actually reachable. We could be in any arm of
|
|
|
|
// ```
|
|
|
|
// match Some(x) {
|
|
|
|
// Some(y) => /* stuff */,
|
|
|
|
// None => /* other */,
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// In surface rust, the current statement would be unreachable.
|
|
|
|
//
|
|
|
|
// However, from the reference chapter on enums and RFC 2195,
|
|
|
|
// accessing the wrong variant is not UB if the enum has repr.
|
|
|
|
// So it's not impossible for a series of MIR opts to generate
|
|
|
|
// a downcast to an inactive variant.
|
2023-09-16 09:35:46 +00:00
|
|
|
&& written_variant == read_variant
|
|
|
|
{
|
|
|
|
return Some(fields[f.as_usize()]);
|
|
|
|
}
|
|
|
|
ProjectionElem::Field(f, ty)
|
|
|
|
}
|
2023-09-16 09:16:04 +00:00
|
|
|
ProjectionElem::Index(idx) => {
|
2023-09-16 09:36:16 +00:00
|
|
|
if let Value::Repeat(inner, _) = self.get(value) {
|
|
|
|
return Some(*inner);
|
|
|
|
}
|
2023-09-16 09:16:04 +00:00
|
|
|
let idx = self.locals[idx]?;
|
|
|
|
ProjectionElem::Index(idx)
|
|
|
|
}
|
|
|
|
ProjectionElem::ConstantIndex { offset, min_length, from_end } => {
|
2023-09-16 09:35:46 +00:00
|
|
|
match self.get(value) {
|
2023-09-16 09:36:16 +00:00
|
|
|
Value::Repeat(inner, _) => {
|
|
|
|
return Some(*inner);
|
|
|
|
}
|
2023-05-03 18:36:53 +00:00
|
|
|
Value::Aggregate(AggregateTy::Array, _, operands) => {
|
2023-09-16 09:35:46 +00:00
|
|
|
let offset = if from_end {
|
|
|
|
operands.len() - offset as usize
|
|
|
|
} else {
|
|
|
|
offset as usize
|
|
|
|
};
|
|
|
|
return operands.get(offset).copied();
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
};
|
2023-09-16 09:16:04 +00:00
|
|
|
ProjectionElem::ConstantIndex { offset, min_length, from_end }
|
|
|
|
}
|
|
|
|
ProjectionElem::Subslice { from, to, from_end } => {
|
|
|
|
ProjectionElem::Subslice { from, to, from_end }
|
|
|
|
}
|
|
|
|
ProjectionElem::OpaqueCast(ty) => ProjectionElem::OpaqueCast(ty),
|
|
|
|
ProjectionElem::Subtype(ty) => ProjectionElem::Subtype(ty),
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(self.insert(Value::Projection(value, proj)))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Simplify the projection chain if we know better.
|
|
|
|
#[instrument(level = "trace", skip(self))]
|
|
|
|
fn simplify_place_projection(&mut self, place: &mut Place<'tcx>, location: Location) {
|
|
|
|
// If the projection is indirect, we treat the local as a value, so can replace it with
|
|
|
|
// another local.
|
|
|
|
if place.is_indirect()
|
|
|
|
&& let Some(base) = self.locals[place.local]
|
|
|
|
&& let Some(new_local) = self.try_as_local(base, location)
|
2023-12-24 13:00:48 +00:00
|
|
|
&& place.local != new_local
|
2023-09-16 09:16:04 +00:00
|
|
|
{
|
|
|
|
place.local = new_local;
|
|
|
|
self.reused_locals.insert(new_local);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut projection = Cow::Borrowed(&place.projection[..]);
|
|
|
|
|
|
|
|
for i in 0..projection.len() {
|
|
|
|
let elem = projection[i];
|
2023-12-24 13:00:48 +00:00
|
|
|
if let ProjectionElem::Index(idx_local) = elem
|
|
|
|
&& let Some(idx) = self.locals[idx_local]
|
2023-09-16 09:16:04 +00:00
|
|
|
{
|
|
|
|
if let Some(offset) = self.evaluated[idx].as_ref()
|
|
|
|
&& let Ok(offset) = self.ecx.read_target_usize(offset)
|
2023-12-17 19:49:48 +00:00
|
|
|
&& let Some(min_length) = offset.checked_add(1)
|
2023-09-16 09:16:04 +00:00
|
|
|
{
|
2023-12-17 19:49:48 +00:00
|
|
|
projection.to_mut()[i] =
|
|
|
|
ProjectionElem::ConstantIndex { offset, min_length, from_end: false };
|
2023-12-24 13:00:48 +00:00
|
|
|
} else if let Some(new_idx_local) = self.try_as_local(idx, location)
|
|
|
|
&& idx_local != new_idx_local
|
|
|
|
{
|
|
|
|
projection.to_mut()[i] = ProjectionElem::Index(new_idx_local);
|
|
|
|
self.reused_locals.insert(new_idx_local);
|
2023-09-16 09:16:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if projection.is_owned() {
|
|
|
|
place.projection = self.tcx.mk_place_elems(&projection);
|
|
|
|
}
|
|
|
|
|
|
|
|
trace!(?place);
|
|
|
|
}
|
|
|
|
|
2023-09-26 16:39:39 +00:00
|
|
|
/// Represent the *value* which would be read from `place`, and point `place` to a preexisting
|
|
|
|
/// place with the same value (if that already exists).
|
2023-03-20 18:05:07 +00:00
|
|
|
#[instrument(level = "trace", skip(self), ret)]
|
2023-09-26 16:39:39 +00:00
|
|
|
fn simplify_place_value(
|
|
|
|
&mut self,
|
|
|
|
place: &mut Place<'tcx>,
|
|
|
|
location: Location,
|
|
|
|
) -> Option<VnIndex> {
|
2023-09-16 09:16:04 +00:00
|
|
|
self.simplify_place_projection(place, location);
|
|
|
|
|
2023-09-26 16:39:39 +00:00
|
|
|
// Invariant: `place` and `place_ref` point to the same value, even if they point to
|
|
|
|
// different memory locations.
|
2023-04-12 18:39:56 +00:00
|
|
|
let mut place_ref = place.as_ref();
|
2023-03-20 18:05:07 +00:00
|
|
|
|
2023-09-26 16:39:39 +00:00
|
|
|
// Invariant: `value` holds the value up-to the `index`th projection excluded.
|
|
|
|
let mut value = self.locals[place.local]?;
|
2023-03-20 18:05:07 +00:00
|
|
|
for (index, proj) in place.projection.iter().enumerate() {
|
2023-04-12 18:39:56 +00:00
|
|
|
if let Some(local) = self.try_as_local(value, location) {
|
2023-09-26 16:39:39 +00:00
|
|
|
// Both `local` and `Place { local: place.local, projection: projection[..index] }`
|
|
|
|
// hold the same value. Therefore, following place holds the value in the original
|
|
|
|
// `place`.
|
2023-04-12 18:39:56 +00:00
|
|
|
place_ref = PlaceRef { local, projection: &place.projection[index..] };
|
|
|
|
}
|
|
|
|
|
2023-09-16 09:16:04 +00:00
|
|
|
let base = PlaceRef { local: place.local, projection: &place.projection[..index] };
|
|
|
|
value = self.project(base, value, proj)?;
|
2023-03-20 18:05:07 +00:00
|
|
|
}
|
|
|
|
|
2023-09-16 09:16:04 +00:00
|
|
|
if let Some(new_local) = self.try_as_local(value, location) {
|
|
|
|
place_ref = PlaceRef { local: new_local, projection: &[] };
|
|
|
|
}
|
|
|
|
|
|
|
|
if place_ref.local != place.local || place_ref.projection.len() < place.projection.len() {
|
2023-09-26 16:39:39 +00:00
|
|
|
// By the invariant on `place_ref`.
|
2023-04-12 18:39:56 +00:00
|
|
|
*place = place_ref.project_deeper(&[], self.tcx);
|
|
|
|
self.reused_locals.insert(place_ref.local);
|
|
|
|
}
|
|
|
|
|
2023-03-20 18:05:07 +00:00
|
|
|
Some(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[instrument(level = "trace", skip(self), ret)]
|
2023-04-12 18:39:56 +00:00
|
|
|
fn simplify_operand(
|
|
|
|
&mut self,
|
|
|
|
operand: &mut Operand<'tcx>,
|
|
|
|
location: Location,
|
|
|
|
) -> Option<VnIndex> {
|
2023-03-20 18:05:07 +00:00
|
|
|
match *operand {
|
2023-09-16 09:16:04 +00:00
|
|
|
Operand::Constant(ref mut constant) => {
|
|
|
|
let const_ = constant.const_.normalize(self.tcx, self.param_env);
|
2023-10-14 12:12:54 +00:00
|
|
|
self.insert_constant(const_)
|
2023-09-16 09:16:04 +00:00
|
|
|
}
|
2023-04-12 18:39:56 +00:00
|
|
|
Operand::Copy(ref mut place) | Operand::Move(ref mut place) => {
|
2023-09-26 16:39:39 +00:00
|
|
|
let value = self.simplify_place_value(place, location)?;
|
2023-04-12 18:39:56 +00:00
|
|
|
if let Some(const_) = self.try_as_constant(value) {
|
|
|
|
*operand = Operand::Constant(Box::new(const_));
|
|
|
|
}
|
|
|
|
Some(value)
|
|
|
|
}
|
2023-03-20 18:05:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[instrument(level = "trace", skip(self), ret)]
|
2023-04-12 18:39:56 +00:00
|
|
|
fn simplify_rvalue(
|
|
|
|
&mut self,
|
|
|
|
rvalue: &mut Rvalue<'tcx>,
|
|
|
|
location: Location,
|
|
|
|
) -> Option<VnIndex> {
|
2023-03-20 18:05:07 +00:00
|
|
|
let value = match *rvalue {
|
|
|
|
// Forward values.
|
2023-04-12 18:39:56 +00:00
|
|
|
Rvalue::Use(ref mut operand) => return self.simplify_operand(operand, location),
|
|
|
|
Rvalue::CopyForDeref(place) => {
|
|
|
|
let mut operand = Operand::Copy(place);
|
|
|
|
let val = self.simplify_operand(&mut operand, location);
|
|
|
|
*rvalue = Rvalue::Use(operand);
|
|
|
|
return val;
|
|
|
|
}
|
2023-03-20 18:05:07 +00:00
|
|
|
|
|
|
|
// Roots.
|
2023-04-12 18:39:56 +00:00
|
|
|
Rvalue::Repeat(ref mut op, amount) => {
|
|
|
|
let op = self.simplify_operand(op, location)?;
|
2023-03-20 18:05:07 +00:00
|
|
|
Value::Repeat(op, amount)
|
|
|
|
}
|
|
|
|
Rvalue::NullaryOp(op, ty) => Value::NullaryOp(op, ty),
|
2023-10-14 12:12:54 +00:00
|
|
|
Rvalue::Aggregate(..) => return self.simplify_aggregate(rvalue, location),
|
2023-09-16 09:16:04 +00:00
|
|
|
Rvalue::Ref(_, borrow_kind, ref mut place) => {
|
|
|
|
self.simplify_place_projection(place, location);
|
|
|
|
return self.new_pointer(*place, AddressKind::Ref(borrow_kind));
|
2023-09-19 20:12:48 +00:00
|
|
|
}
|
2023-09-16 09:16:04 +00:00
|
|
|
Rvalue::AddressOf(mutbl, ref mut place) => {
|
|
|
|
self.simplify_place_projection(place, location);
|
|
|
|
return self.new_pointer(*place, AddressKind::Address(mutbl));
|
2023-09-19 20:12:48 +00:00
|
|
|
}
|
2023-03-20 18:05:07 +00:00
|
|
|
|
|
|
|
// Operations.
|
2023-03-20 20:15:59 +00:00
|
|
|
Rvalue::Len(ref mut place) => return self.simplify_len(place, location),
|
2023-10-11 19:39:18 +00:00
|
|
|
Rvalue::Cast(ref mut kind, ref mut value, to) => {
|
|
|
|
return self.simplify_cast(kind, value, to, location);
|
2023-03-20 18:05:07 +00:00
|
|
|
}
|
2023-04-12 18:39:56 +00:00
|
|
|
Rvalue::BinaryOp(op, box (ref mut lhs, ref mut rhs)) => {
|
2023-03-20 21:37:36 +00:00
|
|
|
let ty = lhs.ty(self.local_decls, self.tcx);
|
2023-04-12 18:39:56 +00:00
|
|
|
let lhs = self.simplify_operand(lhs, location);
|
|
|
|
let rhs = self.simplify_operand(rhs, location);
|
2024-01-08 22:44:38 +00:00
|
|
|
// Only short-circuit options after we called `simplify_operand`
|
|
|
|
// on both operands for side effect.
|
2023-03-20 21:37:36 +00:00
|
|
|
let lhs = lhs?;
|
|
|
|
let rhs = rhs?;
|
|
|
|
if let Some(value) = self.simplify_binary(op, false, ty, lhs, rhs) {
|
|
|
|
return Some(value);
|
|
|
|
}
|
|
|
|
Value::BinaryOp(op, lhs, rhs)
|
2023-03-20 18:05:07 +00:00
|
|
|
}
|
2023-04-12 18:39:56 +00:00
|
|
|
Rvalue::CheckedBinaryOp(op, box (ref mut lhs, ref mut rhs)) => {
|
2023-03-20 21:37:36 +00:00
|
|
|
let ty = lhs.ty(self.local_decls, self.tcx);
|
2023-04-12 18:39:56 +00:00
|
|
|
let lhs = self.simplify_operand(lhs, location);
|
|
|
|
let rhs = self.simplify_operand(rhs, location);
|
2024-01-08 22:44:38 +00:00
|
|
|
// Only short-circuit options after we called `simplify_operand`
|
|
|
|
// on both operands for side effect.
|
2023-03-20 21:37:36 +00:00
|
|
|
let lhs = lhs?;
|
|
|
|
let rhs = rhs?;
|
|
|
|
if let Some(value) = self.simplify_binary(op, true, ty, lhs, rhs) {
|
|
|
|
return Some(value);
|
|
|
|
}
|
|
|
|
Value::CheckedBinaryOp(op, lhs, rhs)
|
2023-03-20 18:05:07 +00:00
|
|
|
}
|
2023-04-12 18:39:56 +00:00
|
|
|
Rvalue::UnaryOp(op, ref mut arg) => {
|
|
|
|
let arg = self.simplify_operand(arg, location)?;
|
2023-03-25 22:33:35 +00:00
|
|
|
if let Some(value) = self.simplify_unary(op, arg) {
|
|
|
|
return Some(value);
|
|
|
|
}
|
2023-03-20 18:05:07 +00:00
|
|
|
Value::UnaryOp(op, arg)
|
|
|
|
}
|
2023-04-12 18:39:56 +00:00
|
|
|
Rvalue::Discriminant(ref mut place) => {
|
2023-09-26 16:39:39 +00:00
|
|
|
let place = self.simplify_place_value(place, location)?;
|
2023-09-16 09:35:46 +00:00
|
|
|
if let Some(discr) = self.simplify_discriminant(place) {
|
|
|
|
return Some(discr);
|
|
|
|
}
|
2023-03-20 18:05:07 +00:00
|
|
|
Value::Discriminant(place)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unsupported values.
|
|
|
|
Rvalue::ThreadLocalRef(..) | Rvalue::ShallowInitBox(..) => return None,
|
|
|
|
};
|
|
|
|
debug!(?value);
|
|
|
|
Some(self.insert(value))
|
|
|
|
}
|
2023-09-16 09:35:46 +00:00
|
|
|
|
|
|
|
fn simplify_discriminant(&mut self, place: VnIndex) -> Option<VnIndex> {
|
|
|
|
if let Value::Aggregate(enum_ty, variant, _) = *self.get(place)
|
2024-02-12 15:39:32 +09:00
|
|
|
&& let AggregateTy::Def(enum_did, enum_args) = enum_ty
|
2023-05-03 18:36:53 +00:00
|
|
|
&& let DefKind::Enum = self.tcx.def_kind(enum_did)
|
2023-09-16 09:35:46 +00:00
|
|
|
{
|
2024-02-12 15:39:32 +09:00
|
|
|
let enum_ty = self.tcx.type_of(enum_did).instantiate(self.tcx, enum_args);
|
2023-09-16 09:35:46 +00:00
|
|
|
let discr = self.ecx.discriminant_for_variant(enum_ty, variant).ok()?;
|
|
|
|
return Some(self.insert_scalar(discr.to_scalar(), discr.layout.ty));
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
2023-05-21 12:59:38 +00:00
|
|
|
|
|
|
|
fn simplify_aggregate(
|
|
|
|
&mut self,
|
|
|
|
rvalue: &mut Rvalue<'tcx>,
|
|
|
|
location: Location,
|
2023-10-14 12:12:54 +00:00
|
|
|
) -> Option<VnIndex> {
|
2023-05-21 12:59:38 +00:00
|
|
|
let Rvalue::Aggregate(box ref kind, ref mut fields) = *rvalue else { bug!() };
|
|
|
|
|
|
|
|
let tcx = self.tcx;
|
|
|
|
if fields.is_empty() {
|
|
|
|
let is_zst = match *kind {
|
2024-01-24 18:01:56 +00:00
|
|
|
AggregateKind::Array(..)
|
|
|
|
| AggregateKind::Tuple
|
|
|
|
| AggregateKind::Closure(..)
|
|
|
|
| AggregateKind::CoroutineClosure(..) => true,
|
2023-05-21 12:59:38 +00:00
|
|
|
// Only enums can be non-ZST.
|
|
|
|
AggregateKind::Adt(did, ..) => tcx.def_kind(did) != DefKind::Enum,
|
|
|
|
// Coroutines are never ZST, as they at least contain the implicit states.
|
|
|
|
AggregateKind::Coroutine(..) => false,
|
|
|
|
};
|
|
|
|
|
|
|
|
if is_zst {
|
|
|
|
let ty = rvalue.ty(self.local_decls, tcx);
|
2023-10-14 12:12:54 +00:00
|
|
|
return self.insert_constant(Const::zero_sized(ty));
|
2023-05-21 12:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let (ty, variant_index) = match *kind {
|
|
|
|
AggregateKind::Array(..) => {
|
|
|
|
assert!(!fields.is_empty());
|
|
|
|
(AggregateTy::Array, FIRST_VARIANT)
|
|
|
|
}
|
|
|
|
AggregateKind::Tuple => {
|
|
|
|
assert!(!fields.is_empty());
|
|
|
|
(AggregateTy::Tuple, FIRST_VARIANT)
|
|
|
|
}
|
2024-02-12 15:39:32 +09:00
|
|
|
AggregateKind::Closure(did, args)
|
|
|
|
| AggregateKind::CoroutineClosure(did, args)
|
|
|
|
| AggregateKind::Coroutine(did, args) => (AggregateTy::Def(did, args), FIRST_VARIANT),
|
|
|
|
AggregateKind::Adt(did, variant_index, args, _, None) => {
|
|
|
|
(AggregateTy::Def(did, args), variant_index)
|
2023-05-21 12:59:38 +00:00
|
|
|
}
|
|
|
|
// Do not track unions.
|
|
|
|
AggregateKind::Adt(_, _, _, _, Some(_)) => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let fields: Option<Vec<_>> = fields
|
|
|
|
.iter_mut()
|
|
|
|
.map(|op| self.simplify_operand(op, location).or_else(|| self.new_opaque()))
|
|
|
|
.collect();
|
|
|
|
let fields = fields?;
|
|
|
|
|
2023-11-13 08:24:55 -05:00
|
|
|
if let AggregateTy::Array = ty
|
|
|
|
&& fields.len() > 4
|
|
|
|
{
|
2023-09-23 16:23:37 +00:00
|
|
|
let first = fields[0];
|
|
|
|
if fields.iter().all(|&v| v == first) {
|
|
|
|
let len = ty::Const::from_target_usize(self.tcx, fields.len().try_into().unwrap());
|
|
|
|
if let Some(const_) = self.try_as_constant(first) {
|
|
|
|
*rvalue = Rvalue::Repeat(Operand::Constant(Box::new(const_)), len);
|
|
|
|
} else if let Some(local) = self.try_as_local(first, location) {
|
|
|
|
*rvalue = Rvalue::Repeat(Operand::Copy(local.into()), len);
|
|
|
|
self.reused_locals.insert(local);
|
|
|
|
}
|
2023-10-14 12:12:54 +00:00
|
|
|
return Some(self.insert(Value::Repeat(first, len)));
|
2023-09-23 16:23:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-14 12:12:54 +00:00
|
|
|
Some(self.insert(Value::Aggregate(ty, variant_index, fields)))
|
2023-05-21 12:59:38 +00:00
|
|
|
}
|
2023-03-20 21:37:36 +00:00
|
|
|
|
2023-03-25 22:33:35 +00:00
|
|
|
#[instrument(level = "trace", skip(self), ret)]
|
|
|
|
fn simplify_unary(&mut self, op: UnOp, value: VnIndex) -> Option<VnIndex> {
|
|
|
|
let value = match (op, self.get(value)) {
|
|
|
|
(UnOp::Not, Value::UnaryOp(UnOp::Not, inner)) => return Some(*inner),
|
|
|
|
(UnOp::Neg, Value::UnaryOp(UnOp::Neg, inner)) => return Some(*inner),
|
|
|
|
(UnOp::Not, Value::BinaryOp(BinOp::Eq, lhs, rhs)) => {
|
|
|
|
Value::BinaryOp(BinOp::Ne, *lhs, *rhs)
|
|
|
|
}
|
|
|
|
(UnOp::Not, Value::BinaryOp(BinOp::Ne, lhs, rhs)) => {
|
|
|
|
Value::BinaryOp(BinOp::Eq, *lhs, *rhs)
|
|
|
|
}
|
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(self.insert(value))
|
|
|
|
}
|
|
|
|
|
2023-03-20 21:37:36 +00:00
|
|
|
#[instrument(level = "trace", skip(self), ret)]
|
|
|
|
fn simplify_binary(
|
|
|
|
&mut self,
|
|
|
|
op: BinOp,
|
|
|
|
checked: bool,
|
|
|
|
lhs_ty: Ty<'tcx>,
|
|
|
|
lhs: VnIndex,
|
|
|
|
rhs: VnIndex,
|
|
|
|
) -> Option<VnIndex> {
|
|
|
|
// Floats are weird enough that none of the logic below applies.
|
|
|
|
let reasonable_ty =
|
|
|
|
lhs_ty.is_integral() || lhs_ty.is_bool() || lhs_ty.is_char() || lhs_ty.is_any_ptr();
|
|
|
|
if !reasonable_ty {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let layout = self.ecx.layout_of(lhs_ty).ok()?;
|
|
|
|
|
|
|
|
let as_bits = |value| {
|
|
|
|
let constant = self.evaluated[value].as_ref()?;
|
2023-10-31 19:29:08 +00:00
|
|
|
if layout.abi.is_scalar() {
|
|
|
|
let scalar = self.ecx.read_scalar(constant).ok()?;
|
|
|
|
scalar.to_bits(constant.layout.size).ok()
|
|
|
|
} else {
|
|
|
|
// `constant` is a wide pointer. Do not evaluate to bits.
|
|
|
|
None
|
|
|
|
}
|
2023-03-20 21:37:36 +00:00
|
|
|
};
|
|
|
|
|
2024-01-08 22:54:05 +00:00
|
|
|
// Represent the values as `Left(bits)` or `Right(VnIndex)`.
|
|
|
|
use Either::{Left, Right};
|
|
|
|
let a = as_bits(lhs).map_or(Right(lhs), Left);
|
|
|
|
let b = as_bits(rhs).map_or(Right(rhs), Left);
|
2023-03-20 21:37:36 +00:00
|
|
|
let result = match (op, a, b) {
|
|
|
|
// Neutral elements.
|
2024-01-08 22:54:05 +00:00
|
|
|
(BinOp::Add | BinOp::BitOr | BinOp::BitXor, Left(0), Right(p))
|
2023-03-20 21:37:36 +00:00
|
|
|
| (
|
|
|
|
BinOp::Add
|
|
|
|
| BinOp::BitOr
|
|
|
|
| BinOp::BitXor
|
|
|
|
| BinOp::Sub
|
|
|
|
| BinOp::Offset
|
|
|
|
| BinOp::Shl
|
|
|
|
| BinOp::Shr,
|
2024-01-08 22:54:05 +00:00
|
|
|
Right(p),
|
|
|
|
Left(0),
|
2023-03-20 21:37:36 +00:00
|
|
|
)
|
2024-01-08 22:54:05 +00:00
|
|
|
| (BinOp::Mul, Left(1), Right(p))
|
|
|
|
| (BinOp::Mul | BinOp::Div, Right(p), Left(1)) => p,
|
2023-03-20 21:37:36 +00:00
|
|
|
// Attempt to simplify `x & ALL_ONES` to `x`, with `ALL_ONES` depending on type size.
|
2024-01-08 22:54:05 +00:00
|
|
|
(BinOp::BitAnd, Right(p), Left(ones)) | (BinOp::BitAnd, Left(ones), Right(p))
|
2023-03-20 21:37:36 +00:00
|
|
|
if ones == layout.size.truncate(u128::MAX)
|
|
|
|
|| (layout.ty.is_bool() && ones == 1) =>
|
|
|
|
{
|
|
|
|
p
|
|
|
|
}
|
|
|
|
// Absorbing elements.
|
2024-01-08 22:54:05 +00:00
|
|
|
(BinOp::Mul | BinOp::BitAnd, _, Left(0))
|
|
|
|
| (BinOp::Rem, _, Left(1))
|
2023-03-20 21:37:36 +00:00
|
|
|
| (
|
|
|
|
BinOp::Mul | BinOp::Div | BinOp::Rem | BinOp::BitAnd | BinOp::Shl | BinOp::Shr,
|
2024-01-08 22:54:05 +00:00
|
|
|
Left(0),
|
2023-03-20 21:37:36 +00:00
|
|
|
_,
|
|
|
|
) => self.insert_scalar(Scalar::from_uint(0u128, layout.size), lhs_ty),
|
|
|
|
// Attempt to simplify `x | ALL_ONES` to `ALL_ONES`.
|
2024-01-08 22:54:05 +00:00
|
|
|
(BinOp::BitOr, _, Left(ones)) | (BinOp::BitOr, Left(ones), _)
|
2023-03-20 21:37:36 +00:00
|
|
|
if ones == layout.size.truncate(u128::MAX)
|
|
|
|
|| (layout.ty.is_bool() && ones == 1) =>
|
|
|
|
{
|
|
|
|
self.insert_scalar(Scalar::from_uint(ones, layout.size), lhs_ty)
|
|
|
|
}
|
|
|
|
// Sub/Xor with itself.
|
|
|
|
(BinOp::Sub | BinOp::BitXor, a, b) if a == b => {
|
|
|
|
self.insert_scalar(Scalar::from_uint(0u128, layout.size), lhs_ty)
|
|
|
|
}
|
|
|
|
// Comparison:
|
|
|
|
// - if both operands can be computed as bits, just compare the bits;
|
|
|
|
// - if we proved that both operands have the same value, we can insert true/false;
|
|
|
|
// - otherwise, do nothing, as we do not try to prove inequality.
|
2024-01-08 22:54:05 +00:00
|
|
|
(BinOp::Eq, Left(a), Left(b)) => self.insert_bool(a == b),
|
|
|
|
(BinOp::Eq, a, b) if a == b => self.insert_bool(true),
|
|
|
|
(BinOp::Ne, Left(a), Left(b)) => self.insert_bool(a != b),
|
|
|
|
(BinOp::Ne, a, b) if a == b => self.insert_bool(false),
|
2023-03-20 21:37:36 +00:00
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
if checked {
|
|
|
|
let false_val = self.insert_bool(false);
|
|
|
|
Some(self.insert_tuple(vec![result, false_val]))
|
|
|
|
} else {
|
|
|
|
Some(result)
|
|
|
|
}
|
|
|
|
}
|
2023-03-20 20:15:59 +00:00
|
|
|
|
2023-10-11 19:39:18 +00:00
|
|
|
fn simplify_cast(
|
|
|
|
&mut self,
|
|
|
|
kind: &mut CastKind,
|
|
|
|
operand: &mut Operand<'tcx>,
|
|
|
|
to: Ty<'tcx>,
|
|
|
|
location: Location,
|
|
|
|
) -> Option<VnIndex> {
|
|
|
|
use rustc_middle::ty::adjustment::PointerCoercion::*;
|
|
|
|
use CastKind::*;
|
|
|
|
|
|
|
|
let mut from = operand.ty(self.local_decls, self.tcx);
|
|
|
|
let mut value = self.simplify_operand(operand, location)?;
|
2023-12-30 23:37:50 +00:00
|
|
|
if from == to {
|
|
|
|
return Some(value);
|
|
|
|
}
|
2023-10-11 19:39:18 +00:00
|
|
|
|
|
|
|
if let CastKind::PointerCoercion(ReifyFnPointer | ClosureFnPointer(_)) = kind {
|
|
|
|
// Each reification of a generic fn may get a different pointer.
|
|
|
|
// Do not try to merge them.
|
|
|
|
return self.new_opaque();
|
|
|
|
}
|
|
|
|
|
|
|
|
if let PtrToPtr | PointerCoercion(MutToConstPointer) = kind
|
|
|
|
&& let Value::Cast { kind: inner_kind, value: inner_value, from: inner_from, to: _ } =
|
|
|
|
*self.get(value)
|
|
|
|
&& let PtrToPtr | PointerCoercion(MutToConstPointer) = inner_kind
|
|
|
|
{
|
|
|
|
from = inner_from;
|
|
|
|
value = inner_value;
|
|
|
|
*kind = PtrToPtr;
|
|
|
|
if inner_from == to {
|
|
|
|
return Some(inner_value);
|
|
|
|
}
|
|
|
|
if let Some(const_) = self.try_as_constant(value) {
|
|
|
|
*operand = Operand::Constant(Box::new(const_));
|
|
|
|
} else if let Some(local) = self.try_as_local(value, location) {
|
|
|
|
*operand = Operand::Copy(local.into());
|
|
|
|
self.reused_locals.insert(local);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(self.insert(Value::Cast { kind: *kind, value, from, to }))
|
|
|
|
}
|
|
|
|
|
2023-03-20 20:15:59 +00:00
|
|
|
fn simplify_len(&mut self, place: &mut Place<'tcx>, location: Location) -> Option<VnIndex> {
|
|
|
|
// Trivial case: we are fetching a statically known length.
|
|
|
|
let place_ty = place.ty(self.local_decls, self.tcx).ty;
|
|
|
|
if let ty::Array(_, len) = place_ty.kind() {
|
|
|
|
return self.insert_constant(Const::from_ty_const(*len, self.tcx));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut inner = self.simplify_place_value(place, location)?;
|
|
|
|
|
|
|
|
// The length information is stored in the fat pointer.
|
|
|
|
// Reborrowing copies length information from one pointer to the other.
|
|
|
|
while let Value::Address { place: borrowed, .. } = self.get(inner)
|
|
|
|
&& let [PlaceElem::Deref] = borrowed.projection[..]
|
|
|
|
&& let Some(borrowed) = self.locals[borrowed.local]
|
|
|
|
{
|
|
|
|
inner = borrowed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have an unsizing cast, which assigns the length to fat pointer metadata.
|
|
|
|
if let Value::Cast { kind, from, to, .. } = self.get(inner)
|
|
|
|
&& let CastKind::PointerCoercion(ty::adjustment::PointerCoercion::Unsize) = kind
|
|
|
|
&& let Some(from) = from.builtin_deref(true)
|
|
|
|
&& let ty::Array(_, len) = from.ty.kind()
|
|
|
|
&& let Some(to) = to.builtin_deref(true)
|
|
|
|
&& let ty::Slice(..) = to.ty.kind()
|
|
|
|
{
|
|
|
|
return self.insert_constant(Const::from_ty_const(*len, self.tcx));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fallback: a symbolic `Len`.
|
|
|
|
Some(self.insert(Value::Len(inner)))
|
|
|
|
}
|
2023-03-20 18:05:07 +00:00
|
|
|
}
|
|
|
|
|
2023-09-19 20:12:48 +00:00
|
|
|
fn op_to_prop_const<'tcx>(
|
|
|
|
ecx: &mut InterpCx<'_, 'tcx, DummyMachine>,
|
|
|
|
op: &OpTy<'tcx>,
|
|
|
|
) -> Option<ConstValue<'tcx>> {
|
|
|
|
// Do not attempt to propagate unsized locals.
|
|
|
|
if op.layout.is_unsized() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This constant is a ZST, just return an empty value.
|
|
|
|
if op.layout.is_zst() {
|
|
|
|
return Some(ConstValue::ZeroSized);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not synthetize too large constants. Codegen will just memcpy them, which we'd like to avoid.
|
|
|
|
if !matches!(op.layout.abi, Abi::Scalar(..) | Abi::ScalarPair(..)) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this constant has scalar ABI, return it as a `ConstValue::Scalar`.
|
|
|
|
if let Abi::Scalar(abi::Scalar::Initialized { .. }) = op.layout.abi
|
|
|
|
&& let Ok(scalar) = ecx.read_scalar(op)
|
2023-10-10 15:42:23 +00:00
|
|
|
&& scalar.try_to_int().is_ok()
|
2023-09-19 20:12:48 +00:00
|
|
|
{
|
|
|
|
return Some(ConstValue::Scalar(scalar));
|
|
|
|
}
|
|
|
|
|
2023-10-23 18:27:54 +00:00
|
|
|
// If this constant is already represented as an `Allocation`,
|
|
|
|
// try putting it into global memory to return it.
|
2023-10-12 16:15:52 +00:00
|
|
|
if let Either::Left(mplace) = op.as_mplace_or_imm() {
|
2023-10-10 15:42:23 +00:00
|
|
|
let (size, _align) = ecx.size_and_align_of_mplace(&mplace).ok()??;
|
|
|
|
|
|
|
|
// Do not try interning a value that contains provenance.
|
2023-10-23 18:27:54 +00:00
|
|
|
// Due to https://github.com/rust-lang/rust/issues/79738, doing so could lead to bugs.
|
2023-10-27 20:51:25 +02:00
|
|
|
// FIXME: remove this hack once that issue is fixed.
|
2023-10-10 15:42:23 +00:00
|
|
|
let alloc_ref = ecx.get_ptr_alloc(mplace.ptr(), size).ok()??;
|
|
|
|
if alloc_ref.has_provenance() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2023-09-19 20:12:48 +00:00
|
|
|
let pointer = mplace.ptr().into_pointer_or_addr().ok()?;
|
2023-11-25 18:41:53 +01:00
|
|
|
let (prov, offset) = pointer.into_parts();
|
|
|
|
let alloc_id = prov.alloc_id();
|
2023-10-12 16:17:06 +00:00
|
|
|
intern_const_alloc_for_constprop(ecx, alloc_id).ok()?;
|
2023-10-22 14:49:00 +00:00
|
|
|
if matches!(ecx.tcx.global_alloc(alloc_id), GlobalAlloc::Memory(_)) {
|
|
|
|
// `alloc_id` may point to a static. Codegen will choke on an `Indirect` with anything
|
|
|
|
// by `GlobalAlloc::Memory`, so do fall through to copying if needed.
|
2023-10-27 20:51:25 +02:00
|
|
|
// FIXME: find a way to treat this more uniformly
|
|
|
|
// (probably by fixing codegen)
|
2023-10-22 14:49:00 +00:00
|
|
|
return Some(ConstValue::Indirect { alloc_id, offset });
|
|
|
|
}
|
2023-09-19 20:12:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Everything failed: create a new allocation to hold the data.
|
|
|
|
let alloc_id =
|
|
|
|
ecx.intern_with_temp_alloc(op.layout, |ecx, dest| ecx.copy_op(op, dest, false)).ok()?;
|
2023-10-10 15:42:23 +00:00
|
|
|
let value = ConstValue::Indirect { alloc_id, offset: Size::ZERO };
|
|
|
|
|
2023-10-23 18:27:54 +00:00
|
|
|
// Check that we do not leak a pointer.
|
|
|
|
// Those pointers may lose part of their identity in codegen.
|
2023-10-27 20:51:25 +02:00
|
|
|
// FIXME: remove this hack once https://github.com/rust-lang/rust/issues/79738 is fixed.
|
2023-10-23 19:37:01 +00:00
|
|
|
if ecx.tcx.global_alloc(alloc_id).unwrap_memory().inner().provenance().ptrs().is_empty() {
|
2023-10-10 15:42:23 +00:00
|
|
|
return Some(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
2023-09-19 20:12:48 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 18:39:56 +00:00
|
|
|
impl<'tcx> VnState<'_, 'tcx> {
|
2023-04-09 11:15:48 +00:00
|
|
|
/// If `index` is a `Value::Constant`, return the `Constant` to be put in the MIR.
|
2023-03-20 18:05:07 +00:00
|
|
|
fn try_as_constant(&mut self, index: VnIndex) -> Option<ConstOperand<'tcx>> {
|
2023-09-19 20:12:48 +00:00
|
|
|
// This was already constant in MIR, do not change it.
|
2023-10-14 12:12:54 +00:00
|
|
|
if let Value::Constant { value, disambiguator: _ } = *self.get(index)
|
|
|
|
// If the constant is not deterministic, adding an additional mention of it in MIR will
|
|
|
|
// not give the same value as the former mention.
|
|
|
|
&& value.is_deterministic()
|
|
|
|
{
|
|
|
|
return Some(ConstOperand { span: rustc_span::DUMMY_SP, user_ty: None, const_: value });
|
2023-03-20 18:05:07 +00:00
|
|
|
}
|
2023-09-19 20:12:48 +00:00
|
|
|
|
|
|
|
let op = self.evaluated[index].as_ref()?;
|
|
|
|
if op.layout.is_unsized() {
|
|
|
|
// Do not attempt to propagate unsized locals.
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let value = op_to_prop_const(&mut self.ecx, op)?;
|
|
|
|
|
|
|
|
// Check that we do not leak a pointer.
|
|
|
|
// Those pointers may lose part of their identity in codegen.
|
2023-10-27 20:51:25 +02:00
|
|
|
// FIXME: remove this hack once https://github.com/rust-lang/rust/issues/79738 is fixed.
|
2023-10-23 18:27:54 +00:00
|
|
|
assert!(!value.may_have_provenance(self.tcx, op.layout.size));
|
2023-09-19 20:12:48 +00:00
|
|
|
|
|
|
|
let const_ = Const::Val(value, op.layout.ty);
|
|
|
|
Some(ConstOperand { span: rustc_span::DUMMY_SP, user_ty: None, const_ })
|
2023-03-20 18:05:07 +00:00
|
|
|
}
|
|
|
|
|
2023-04-09 11:15:48 +00:00
|
|
|
/// If there is a local which is assigned `index`, and its assignment strictly dominates `loc`,
|
|
|
|
/// return it.
|
2023-03-20 18:05:07 +00:00
|
|
|
fn try_as_local(&mut self, index: VnIndex, loc: Location) -> Option<Local> {
|
2023-05-03 17:27:10 +00:00
|
|
|
let other = self.rev_locals.get(index)?;
|
2023-03-20 18:05:07 +00:00
|
|
|
other
|
|
|
|
.iter()
|
2023-05-03 17:27:10 +00:00
|
|
|
.find(|&&other| self.ssa.assignment_dominates(self.dominators, other, loc))
|
2023-03-20 18:05:07 +00:00
|
|
|
.copied()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-12 18:39:56 +00:00
|
|
|
impl<'tcx> MutVisitor<'tcx> for VnState<'_, 'tcx> {
|
2023-03-20 18:05:07 +00:00
|
|
|
fn tcx(&self) -> TyCtxt<'tcx> {
|
|
|
|
self.tcx
|
|
|
|
}
|
|
|
|
|
2023-09-16 09:16:04 +00:00
|
|
|
fn visit_place(&mut self, place: &mut Place<'tcx>, _: PlaceContext, location: Location) {
|
|
|
|
self.simplify_place_projection(place, location);
|
|
|
|
}
|
|
|
|
|
2023-03-20 18:05:07 +00:00
|
|
|
fn visit_operand(&mut self, operand: &mut Operand<'tcx>, location: Location) {
|
2023-04-12 18:39:56 +00:00
|
|
|
self.simplify_operand(operand, location);
|
2023-03-20 18:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_statement(&mut self, stmt: &mut Statement<'tcx>, location: Location) {
|
2023-10-31 19:08:36 +00:00
|
|
|
if let StatementKind::Assign(box (ref mut lhs, ref mut rvalue)) = stmt.kind {
|
|
|
|
self.simplify_place_projection(lhs, location);
|
|
|
|
|
2023-09-23 07:56:59 +00:00
|
|
|
// Do not try to simplify a constant, it's already in canonical shape.
|
2023-10-31 19:08:36 +00:00
|
|
|
if matches!(rvalue, Rvalue::Use(Operand::Constant(_))) {
|
|
|
|
return;
|
2023-03-20 18:05:07 +00:00
|
|
|
}
|
2023-10-31 19:08:36 +00:00
|
|
|
|
|
|
|
let value = lhs
|
|
|
|
.as_local()
|
|
|
|
.and_then(|local| self.locals[local])
|
|
|
|
.or_else(|| self.simplify_rvalue(rvalue, location));
|
|
|
|
let Some(value) = value else { return };
|
|
|
|
|
|
|
|
if let Some(const_) = self.try_as_constant(value) {
|
|
|
|
*rvalue = Rvalue::Use(Operand::Constant(Box::new(const_)));
|
|
|
|
} else if let Some(local) = self.try_as_local(value, location)
|
|
|
|
&& *rvalue != Rvalue::Use(Operand::Move(local.into()))
|
|
|
|
{
|
|
|
|
*rvalue = Rvalue::Use(Operand::Copy(local.into()));
|
|
|
|
self.reused_locals.insert(local);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
2023-03-20 18:05:07 +00:00
|
|
|
}
|
2023-10-31 19:08:36 +00:00
|
|
|
self.super_statement(stmt, location);
|
2023-03-20 18:05:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct StorageRemover<'tcx> {
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
reused_locals: BitSet<Local>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> MutVisitor<'tcx> for StorageRemover<'tcx> {
|
|
|
|
fn tcx(&self) -> TyCtxt<'tcx> {
|
|
|
|
self.tcx
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_operand(&mut self, operand: &mut Operand<'tcx>, _: Location) {
|
|
|
|
if let Operand::Move(place) = *operand
|
2024-02-05 23:21:33 +00:00
|
|
|
&& !place.is_indirect_first_projection()
|
|
|
|
&& self.reused_locals.contains(place.local)
|
2023-03-20 18:05:07 +00:00
|
|
|
{
|
|
|
|
*operand = Operand::Copy(place);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_statement(&mut self, stmt: &mut Statement<'tcx>, loc: Location) {
|
|
|
|
match stmt.kind {
|
|
|
|
// When removing storage statements, we need to remove both (#107511).
|
|
|
|
StatementKind::StorageLive(l) | StatementKind::StorageDead(l)
|
|
|
|
if self.reused_locals.contains(l) =>
|
|
|
|
{
|
|
|
|
stmt.make_nop()
|
|
|
|
}
|
|
|
|
_ => self.super_statement(stmt, loc),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|